Forum: RSpec Rails 3.2.1 breaks stub ?

Posted by Larry (Guest)
on 2012-01-28 18:01
(Received via mailing list)
Hi,

First things first: Thanks for the GREAT gem and all of the work that
you and others put into this - it is much appreciated!

I read on David Chelimsky's blog that Rails 3.2 broke "stub_model" and
that upgrading to rspec-rails-2.8.1 would fix the problem.

I have a bunch of specs that ran fine using rspec-rails-2.8.1 and
Rails 3.1.3, but when I upgraded to Rails 3.2.1 the following line
choked for the following reason (which I believe is the same problem
that occurred with "stub_model"):

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

Error: ArgumentError: wrong number of arguments (0 for 2)

Has anyone else seen this? Is there a fix? Am I barking up the wrong
tree?

Thanks,
Larry
Posted by David Chelimsky (Guest)
on 2012-01-28 23:45
(Received via mailing list)
On Sat, Jan 28, 2012 at 8:14 AM, Larry <info@stupidtuesday.com> wrote:
> I have a bunch of specs that ran fine using rspec-rails-2.8.1 and
> Rails 3.1.3, but when I upgraded to Rails 3.2.1 the following line
> choked for the following reason (which I believe is the same problem
> that occurred with "stub_model"):
>
> Code:
> act.stub(:update_attributes).and_raise(ActiveRecord::StaleObjectError)
>
> Error: ArgumentError: wrong number of arguments (0 for 2)

Please run with the --backtrace flag and post the backtrace so we can
see where that error is coming from.
Posted by Larry (Guest)
on 2012-01-29 15:23
(Received via mailing list)
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)

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@stuptues/gems/activerecord-3.2.1/
lib/active_record/errors.rb:104:in `initialize'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@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@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@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@stuptues/gems/actionpack-3.2.1/lib/
abstract_controller/base.rb:167:in `process_action'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@stuptues/gems/activesupport-3.2.1/
lib/active_support/callbacks.rb:405:in `__run_callback'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@stuptues/gems/activesupport-3.2.1/
lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/actionpack-3.2.1/lib/
abstract_controller/callbacks.rb:17:in `process_action'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@stuptues/gems/activesupport-3.2.1/
lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/activesupport-3.2.1/
lib/active_support/notifications.rb:123:in `instrument'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@stuptues/gems/actionpack-3.2.1/lib/
abstract_controller/base.rb:121:in `process'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/actionpack-3.2.1/lib/
abstract_controller/rendering.rb:45:in `process'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@stuptues/gems/actionpack-3.2.1/lib/
action_controller/test_case.rb:464:in `process'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/actionpack-3.2.1/lib/
action_controller/test_case.rb:49:in `process'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@stuptues/gems/devise-2.0.0/lib/
devise/test_helpers.rb:70:in `catch'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/devise-2.0.0/lib/
devise/test_helpers.rb:70:in `_catch_warden'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/devise-2.0.0/lib/
devise/test_helpers.rb:19:in `process'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@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@stuptues/gems/rspec-core-2.8.0/lib/
rspec/core/example.rb:77:in `run'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@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@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@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@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@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@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@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@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@stuptues/gems/rspec-core-2.8.0/lib/
rspec/core/reporter.rb:34:in `report'
/home/larry/.rvm/gems/ruby-1.9.3-p0@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@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@stuptues/gems/rspec-core-2.8.0/lib/
rspec/core/runner.rb:69:in `run'
/home/larry/.rvm/gems/ruby-1.9.3-p0@stuptues/gems/rspec-core-2.8.0/lib/
rspec/core/runner.rb:10:in `block in autorun'
Posted by David Chelimsky (Guest)
on 2012-01-29 16:04
(Received via mailing list)
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/M...
[2] 
https://github.com/rails/rails/blob/master/activer...
[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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.