Reputation: 81
I'm using what is largely a scaffold generated RSpec spec, which is failing but shouldn't be. Here is the spec:
describe "PUT update" do
describe "with valid params" do
it "updates the requested invoice" do
invoice = Invoice.create!
Invoice.any_instance.should_receive(:update_attributes).with({"number" => "MyString" })
put :update, {:id => invoice.id, :invoice => { "number" => "MyString" }}
end
When the spec is run, one Invoice is created in the db, and is updated appropriately. However, I get this message and a fail:
RSpec::Mocks::MockExpectationError: (#<Mocha::ClassMethods::AnyInstance:0x653a9a8>).update_attributes({"number"=>"MyString"})
expected: 1 time with arguments: ({"number"=>"MyString"})
received: 0 times with arguments: ({"number"=>"MyString"})
Why is this failing?
Upvotes: 1
Views: 1331
Reputation: 4249
I had a similar issue and I solved it by using expects instead of should_receive. You might just need to update it to use expects like the following.
describe "PUT update" do
describe "with valid params" do
it "updates the requested invoice" do
invoice = Invoice.create!
Invoice.any_instance.expects(:update_attributes).with({"number" => "MyString" })
put :update, {:id => invoice.id, :invoice => { "number" => "MyString" }}
end
end
end
Upvotes: 0
Reputation: 29419
At the risk of stating the obvious, since your getting a Mocha error, it seems to me you need to either disable Mocha or configure it for use with RSpec.
You can disable a gem by removing it from Gemfile and re-executing bundle install
. Or you can add a 'require: false` argument when specifying the gem so that it doesn't autoload, per Bundler: What does :require => false in a Gemfile mean?
Instructions for configuring Mocha to work with RSpec are in https://relishapp.com/rspec/rspec-core/v/2-14/docs/mock-framework-integration/mock-with-mocha
Upvotes: 1
Reputation: 1046
Mocha, like many other mock frameworks, requires you to put your expectations before running the code under test.
Therefore, in your test, swap the two lines; i.e. call Invoice.create!
after Invoice.any_instance.should_receive
.
Upvotes: -1