Reputation: 191
Using Rhinomocks, how can I verify that a Mock/stub was never called at all? Meaning no methods was called on the mock/stub?
I am aware of the AssertWasNotCalled method, but this method requires that I mention a method name. (Perhaps I have a class with 10 different methods that could be called).
Log.AssertWasNotCalled(x => x.LogAndReportException(null, null), x => x.IgnoreArguments());
Upvotes: 7
Views: 2367
Reputation: 233150
You can use a Strict mock, althought this is a feature that may go away in the future:
var mocks = new MockRepository();
var cm = mocks.StrictMock<ICallMonitor>();
cm.Replay();
cm.HangUp(); // this will cause VerifyAllExpectations to throw
cm.VerifyAllExpectations();
In this syntax, a Strick Mock only allows explicitly defined calls.
Upvotes: 6
Reputation: 234434
When you are using mocks, you should not assert every single call was made or not. That couples your tests to a particular implementation and makes them fragile and a refactoring nightmare.
If I ever ran into this situation I would rethink why I wanted to assert that a dependency was never used.
Obviously, if the dependency is not used anywhere, just remove it. If it is needed for some operations, but all the operations in the dependency are destructive operations and you want to make sure some operation does not do harm with them, you should assert explicitly that the destructive operations were not called and allow the implementation to do whatever it wants with the non-destructive operations (if there are any). This makes your tests more explicit and less fragile.
Upvotes: 1
Reputation: 72860
You can use the StrictMock
method to create a strict mock - this will fail if any unexcepted method call is used. According to Ayende's site, this is discouraged, but it sounds like exactly the scenario where it would be useful.
Upvotes: 1