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.