I have a test which looks a bit like this (I’m simplifying a bit from my real issue):
[Test]
public void Eat_calls_consumption_tracker_OnConsume()
{
var consumptionTrackerStub =
MockRepository.GenerateStub<IConsumptionTracker>();
var monkey = new Monkey(consumptionTrackerStub);
var banana = new Banana();
monkey.Eat(banana);
consumptionTrackerStub.AssertWasCalled(x => x.OnConsume(banana));
}
This would work fine, except that Monkey disposes Banana after eating it. Therefore, the banana object is no longer in a usable state. In particular, the Banana.Equals implementation cannot work after Dispose is called because it uses unmanaged resources that have been released.
Unfortunately AssertWasCalled will cause Banana.Equals to be called, which blows up, causing the test to fail. What is the best way to fix this?
Turns out you can force Rhino Mocks to check the arguments with
object.ReferenceEqualslike this:object.ReferenceEqualsstill works even if the banana has been disposed, thus fixing the problem.