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 C.'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
larry
January 28, 2012, 11:45pm
2
On Sat, Jan 28, 2012 at 8:14 AM, Larry [email protected] 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.
larry
January 29, 2012, 3:23pm
3
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’
larry
January 29, 2012, 4:04pm
4
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]
# Raised when connection to the database could not been established because it was not
# able to connect to the host or when the authorization failed.
class DatabaseConnectionError < ConnectionNotEstablished
def initialize(message = nil)
super(message || "Database connection error")
end
class << self
def hostname_error(hostname)
DatabaseConnectionError.new(<<~MSG)
There is an issue connecting with your hostname: #{hostname}.\n
Please check your database configuration and ensure there is a valid connection to your database.
MSG
end
def username_error(username)
DatabaseConnectionError.new(<<~MSG)
There is an issue connecting to your database with your username/password, username: #{username}.\n
Please check your database configuration to ensure the username/password are valid.
MSG
end
[3] Includes stale record in StaleObjectError · rails/rails@410fa4c · GitHub
[4] Consider attempted action in exception message of ActiveRecord::Stale… · rails/rails@c6f0461 · GitHub
[5] Add a helpful message when an error class passed to `and_raise` requires 1 or more initialization arguments · Issue #99 · rspec/rspec-mocks · GitHub