[rspec-users] testing around_save

David Chelimsky dchelimsky at gmail.com
Fri Feb 24 01:36:46 UTC 2012


On Feb 23, 2012, at 3:36 PM, apneadiving wrote:

> I've just upgraded to Rails 3.2.1 and I can't get my specs checking
> whether or not around_save work anymore.

What version did you upgrade from? Anything else change in the process?

> I simply did something like:
> 
> object.should_receive :around_filter_name
> object.save.should be_true
> 
> (Actually, it was slightly more complex, it's part of a state machine)

Mock objects (message expectations / should_receive) are for specifying interaction _between objects_, not internal implementation of a single object. There are plenty of guidelines that point in that direction, two of which are explicitly violated in this example:

1. mock your own code, not your dependencies (in this case you're mocking ActiveRecord internals)
2. don't mock the subject of a test

The motivation for both of these guidelines is exactly what you're experiencing. Something changed out from under you and you have no idea what and why your test doesn't work anymore. I'd recommend using a black block approach for this instead of a message expectation: get the object into the state you want, call object.save, and then specify the resulting state.

HTH,
David


More information about the rspec-users mailing list