rspec/rspec-mocks — Issue #98: #with fails when passed a #mock_model behaving as a null object
Author: jimryan
Created: 2012-01-25 20:10:43+00:00
State: open

Given a spec like this:

let(:user) { mock_model(User).as_null_object }

before { User.stub_chain(:where, :first).and_return(user) }

it "logs user in" do
UserSession.should_receive(:create).with(user)
post :create
end

And some code:

def create
user = User.find(...).first
UserSession.create(user)
end

The spec will fail, claiming:

received :create with unexpected arguments
expected: ()
got: ()

And this is due to how the `ArgumentExpectation` [determines if the argument is a null object](https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_expectation.rb#L38), which will fail to detect a `mock_model` behaving as a null object.

I'm not exactly clear on why this method was used to determine if the object was a null object, when we have [`#null_object?`](https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/methods.rb#L104), so unless I'm missing something, I suggest we simply use that to determine if the object is a null object.

If not, then we can fix this on the rspec-rails side.

I'm happy to implement my fix, I just wanted someone else with a deeper understanding of RSpec to chime in and make sure my fix and this issue are valid.

Thanks,
Jim