Mocking with Rhino Mocks: Expecting a method not to be called

Mocking with Rhino Mocks: Expecting a method not to be called This morning I thought about trying out some Mocking with Rhino Mocks. First of all I was having some little trouble of testing method's that returned void. Oren Eini suggested somewhere to make use of method chaining, so I changed it to use method chaining. So what's next? Expecting a method not to be called. For example the situation on line 12: I expect the WriteLog method with parameter EventLogEntryType.SuccesAudit not to be called. I think the test I wrote should work.
 1 [Test] 
 2 public void TestInterpretationForAuditFailureNotWorking() 
 3 { 
 4   MockRepository mocks = new MockRepository(); 
 5   ILogWriter eventLogMock = (ILogWriter) mocks.DynamicMock( 
 6                                       typeof (ILogWriter)); 
 7   ILog log = new Log(null, eventLogMock, "MOCK", true, false); 
 8  
 9   Expect.Call(eventLogMock.WriteLog( 
10               EventLogEntryType.FailureAudit, "MOCK", null, null, 0) 
11     ).Return(true); 
12   Expect.Call(eventLogMock.WriteLog( 
13               EventLogEntryType.SuccessAudit, "MOCK", null, null, 0) 
14     ).Return(true).Repeat.Never(); 
15   mocks.ReplayAll(); 
16  
17   log.Audit(Audit.Failure, null, null); 
18   mocks.VerifyAll(); 
19 }
The test actually doesn't work. I get an InvalidOperationException when I call VerifyAll.
System.InvalidOperationException: Can set only a single return value or 
exception to throw or delegate to throw on the same method call.at Rhino.Mocks.Expectations.AbstractExpectation.ActionOnMethodNotSpesified() 
at Rhino.Mocks.Expectations.AbstractExpectation.set_ExceptionToThrow(Exception value) 
at Rhino.Mocks.Impl.MethodOptions.Never() 
Does anyone know how this can be solved? What am I doing wrong? The only thing I want is: Expect a method not to be called. Update: 1 August 2007 After some e-mail contact with Oren he came with the following solution:
12   Expect.Call(eventLogMock.WriteLog( 
13               EventLogEntryType.SuccessAudit, "MOCK", null, null, 0) 
14     ).Return(true).Repeat.Never(); 
The solution mentions not to use Return, because you don't expect anything in return. You expect the method not to be called.
  • Gravatar Ayende Rahien July 30th, 2007 at 21:57
    Does "new Log(null, eventLogMock, “MOCK“, true, false);" this calls to eventLogMock
  • Gravatar Mark Monster July 31st, 2007 at 07:39
    No I do not call eventLogMock in the constructor.



    The "log.Audit(Audit.Failure, null, null);" on line 17 does the call to the eventLogMock.

    The "log.Audit(Audit.Succes, null, null);" that does not exist should not call the eventLogMock because of a value in a config key. I want to evaluate if the interpretation of my config was right.
  • Gravatar Mark Monster August 1st, 2007 at 15:24
    Small update with the solution from Oren alias Ayende.
  • Gravatar jbierman January 14th, 2010 at 22:42
    Awesome little tip! Thank you!
Gravatar