Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhinomocks, how to verify that a stub/mock was never called at all?

Tags:

rhino-mocks

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());
like image 787
Andy Avatar asked Jan 04 '10 12:01

Andy


2 Answers

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.

like image 106
Mark Seemann Avatar answered Oct 01 '22 10:10

Mark Seemann


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.

like image 38
David M Avatar answered Oct 01 '22 11:10

David M