开发者

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 开发者_JAVA百科passes. So I am wondering why you would need to have the verify expectation call at all.

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


When you are performing the unit testing, you are not just testing the expectations of the component that you are testing, you are also testing the expectations of the component you are testing and how it interacts with other components it relies on.

Let's say that you mock a repository & unit of work pattern interfaces and pass mocks of them to your component. While the component might give you the right result if you tell the repository to return certain data, you want to verify that the implementations of the interfaces were called in the way that you expect them to. This is what verification is for.

When combined with testing the results of the processing your component does, you have a much more definitive test of not only what it will do, but how it will interact with the components that it requires to do it.


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.


When removing the Verify, the test isn't really testing much at all (other then possible exceptions that might get generated).

Essentially, you aren't testing the interaction of your tested object with your mock at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜