Search code examples
rspecexpectations

RSpec update_attributes expectation failing, even though update works


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?


Solution

  • 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