Harry13
Harry13

Reputation: 743

Rhino Mocks: Verify all mocks in AAA syntax

When using the old Rhino Mocks record replay syntax, a MockRepository instance is created and all mocks from there. At the end of the unit test the method [MockRepository Instance].VerifyAll() is called to verify all the mocks.

With the current AAA syntax all mocks are created by the static methods on the MockRepository class. At the moment I did not find a better solution than verifying each mock object at the end of the test or on tear down. This is more error prone than the behaviour of the old syntax since sometimes a new mock instance is forgotten to verify.

How do you guys handle this, is there a better way to verify all existing mocks?

Upvotes: 3

Views: 810

Answers (1)

k.m
k.m

Reputation: 31454

(...) is there a better way to verify all existing mocks?

The better way would be to test one thing at time. Perhaps Rhino's move away from verify all to explicit verification was done in such fashion - in order to promote testing single thing at time (and as a result, one verification is all you should ever need).

It's easy to realize that the need to verify multiple mocks probabaly comes from the fact that you want to check multiple behaviors at once (as in, single test). Note that this might be the result of non-optimal design choices few stages earlier, and you might take it (difficulty to write a test) as a warning sign.

At ayende's Rhino Mocks 3.5 guide page, you won't find a single example where two or mocks are used in one test.

On the mock and multiple stubs issue

You use stub to setup environment/requirements, not to verify your code. You never verify stub (like, whether some method was called or properties have certain values). Stub is essentially read-only component used by code you test. As a result of this distinction, stub will never make your test fail, whereas mock can certainly do so. This is why you can see multiple stubs in Rhino example, but always one mock.

Upvotes: 4

Related Questions