tags:

views:

272

answers:

3

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());
+1  A: 

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.

David M
+1  A: 

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.

Martinho Fernandes
+1 In general I agree, particularly when it comes to Fragile Tests. However, this may be a valid request. Imagine an interface with Method Injection. Since a method on the interface takes an injected abstract parameter, you must supply something. Now suppose that you are creating a 'null' implementation of that interface, and the requirement is that the injected parameter (which must be present because of the interface) is never used. A bit of a niche scenario, I admit, but still relevant :)
Mark Seemann
@Mark: yeah, that's an acceptable usage. I knew there was some corner case where it could be useful, I just couldn't come up with one.
Martinho Fernandes
Isn't a unit test on a guard clause a good place to make sure any dependencies inside the method are not called? I am currently writing a unit test for this scenario where I want to make sure the guard clause functions as intended and the mock is never called on.
Maslow
@Maslow: if I was writing a unit test on a guard clause I probably wouldn't test that kind of thing. Not that it is a Bad Idea(TM). It's just that if you follow the "simplest thing that could work principle", you don't need to be that strict in your unit tests. To test that kind of thing I would have only two types of tests: 1) feed it valid input and assert it doesn't throw; 2) feed it invalid input and assert it throws the correct exception.
Martinho Fernandes
+4  A: 

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.

Mark Seemann
If I understand this correctly, in this scenario wouldn't both/either `Replay` and `HangUp` cause the verify to throw?
Maslow
@Maslow: no. `Replay` is a Rhino Mocks extension method that places the mock in replay mode. Before it was in record mode and recorded no calls made on it. When it enters replay mode it will allow exactly that: no calls can be made on it.
Martinho Fernandes
ah, unless I'm missing the extension method or this is from a different version, in my code it was `mocks.Replay(cm);` Then `mocks.VerifyAll();`
Maslow