Search code examples
c#rhino-mocks

Verify expectations on a mock?


In the documentation for Rhino Mocks it states that you must verify expectations on a mock which must be verified/asserted later using either the VerifyAllExpectations() or AssertWasCalled() methods.

However if I comment out the verification the test still passes. So I am wondering why you would need to have the verify expectation call at all.

...
notificationSvc.Expect(o => o.UserIsLoggedOut());       
...
//notificationSvc.VerifyAllExpectations();

Solution

  • Verifying an Expectation is as vital to a test case, as is an Assert statement is for a Test.

    You can write any amount of code without Assert statements in a Test method, It would pass. But the question is - "Is it Testing anything ?"

    The Assert statement(s) are the crux of the Test Case.

    Similarly the Verify methods are the crux of all Expectation calls, without Verify method your test case is as good as a Test case without an Assert statement.

    A System interaction could be verified using Expectations, It's a three step proccess

    1. Setting Expectations: Letting know the mocking framework what interactions are you expecting to be invoked.
    2. Interact or Perform actions:: Perform the actual call which you want to test on the SUT(System Under Test)
    3. Verifying Expections: Asking the mocking framework to Verify all the expectations were met while performing Step 2.