[rspec-users] Rails 3.2.1 breaks stub ?

David Chelimsky dchelimsky at gmail.com
Sun Jan 29 10:02:07 EST 2012


On Jan 29, 2012, at 7:53 AM, Larry wrote:

> Hi David,
> 
> The trace is below.
> 
> Like I said, the line in the spec that causes the error (at least I
> think it does; it works when I comment it out; plus, it fails in all 3
> specs where I have a similar line) is:
> 
> act.stub(:update_attributes).and_raise(ActiveRecord::StaleObjectError)

I didn't notice this before, but the first two lines of the backtrace reveal the problem.

The `and_raise` method only works with an error class if that class has a 0-arg initializer [1]. ActiveRecord::StaleObjectError's initializer requires two arguments [2]. This changed between 3.1 and 3.2 [3,4].

I added a placeholder for a feature request to improve the error messaging in rspec-mocks [5].

To fix the problem (with or without helpful error messages from rspec), it's up to you to change

  act.stub(:update_attributes).and_raise(ActiveRecord::StaleObjectError)

to something like

  act.stub(:update_attributes).and_raise(ActiveRecord::StaleObjectError.new(act, :update))

HTH,
David

[1] http://rubydoc.info/gems/rspec-mocks/RSpec/Mocks/MessageExpectation#and_raise-instance_method
[2] https://github.com/rails/rails/blob/master/activerecord/lib/active_record/errors.rb#L101-112
[3] https://github.com/rails/rails/commit/410fa4cf
[4] https://github.com/rails/rails/commit/c6f0461
[5] https://github.com/rspec/rspec-mocks/issues/99

> 
> The line in the controller that bombs out (about 7 lines down in the
> trace) is:
> 
>    if @act.update_attributes params[:act]
>      flash.now.notice = "Act was updated. (Refresh the list to see
> changes in the table.)"
>    end
> 
> Thanks,
> Larry
> 
> 
> ArgumentError: wrong number of arguments (0 for 2)
> 
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activerecord-3.2.1/
> lib/active_record/errors.rb:104:in `initialize'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-mocks-2.8.0/
> lib/rspec/mocks/message_expectation.rb:181:in `exception'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-mocks-2.8.0/
> lib/rspec/mocks/message_expectation.rb:181:in `raise'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-mocks-2.8.0/
> lib/rspec/mocks/message_expectation.rb:181:in `invoke'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-mocks-2.8.0/
> lib/rspec/mocks/proxy.rb:123:in `message_received'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-mocks-2.8.0/
> lib/rspec/mocks/method_double.rb:92:in `update_attributes'
> /home/larry/RubyMineProjects/StupidTuesday/app/controllers/act/
> acts_controller.rb:49:in `update'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/implicit_render.rb:4:in `send_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> abstract_controller/base.rb:167:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/rendering.rb:10:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> abstract_controller/callbacks.rb:18:in `block in process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/callbacks.rb:458:in
> `_run__4120759382721975549__process_action__1657552574701998049__callbacks'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/callbacks.rb:405:in `__run_callback'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/callbacks.rb:81:in `run_callbacks'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> abstract_controller/callbacks.rb:17:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/rescue.rb:29:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/instrumentation.rb:30:in `block in
> process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/notifications.rb:123:in `block in instrument'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/notifications/instrumenter.rb:20:in `instrument'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activesupport-3.2.1/
> lib/active_support/notifications.rb:123:in `instrument'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/instrumentation.rb:29:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/params_wrapper.rb:205:in `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/activerecord-3.2.1/
> lib/active_record/railties/controller_runtime.rb:18:in
> `process_action'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> abstract_controller/base.rb:121:in `process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> abstract_controller/rendering.rb:45:in `process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/metal/testing.rb:17:in `process_with_new_base_test'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/test_case.rb:464:in `process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/test_case.rb:49:in `process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/devise-2.0.0/lib/
> devise/test_helpers.rb:19:in `block in process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/devise-2.0.0/lib/
> devise/test_helpers.rb:70:in `catch'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/devise-2.0.0/lib/
> devise/test_helpers.rb:70:in `_catch_warden'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/devise-2.0.0/lib/
> devise/test_helpers.rb:19:in `process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/actionpack-3.2.1/lib/
> action_controller/test_case.rb:390:in `put'
> /home/larry/RubyMineProjects/StupidTuesday/spec/controllers/act/
> acts_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example.rb:80:in `instance_eval'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example.rb:80:in `block in run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example.rb:173:in `with_around_hooks'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example.rb:77:in `run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:355:in `block in run_examples'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:351:in `map'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:351:in `run_examples'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:337:in `run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:338:in `block in run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:338:in `map'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/example_group.rb:338:in `run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/command_line.rb:28:in `block (2 levels) in run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/command_line.rb:28:in `map'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/command_line.rb:28:in `block in run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/reporter.rb:34:in `report'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/command_line.rb:25:in `run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/runner.rb:80:in `run_in_process'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/runner.rb:69:in `run'
> /home/larry/.rvm/gems/ruby-1.9.3-p0 at stuptues/gems/rspec-core-2.8.0/lib/
> rspec/core/runner.rb:10:in `block in autorun'
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120129/03a1763b/attachment-0001.html>


More information about the rspec-users mailing list