I am struggling a bit to understand how the new AAA syntax works in Rhino Mocks. Most of my tests look like this:
[Test]
public void Test()
{
//Setup
ISth sth= mocks.DynamicMock<ISth>();
//Expectations
Expect.Call(sth.A()).Return("sth");
mocks.ReplayAll();
//Execution
FunctionBeingTested(sth);
//...asserts, etc
//Verification
mocks.VerifyAll();
}
How would it look like using AAA syntax?
Most probably like this:
To really benefit from the new AAA syntax, you have to change your mind a bit. I try to explain.
There is a major difference in my proposal: there is no “Expect-Verify”. So I propose to not expect the call, because there is a return value. I assume that the method can’t pass the test when it didn’t call
sth.A, because it will miss the correct return value. It will fail at least one of the other asserts.This is actually a good thing.
There is another scenario, where you actually need to check if a method had been called on the mock. Mainly if it returns void. For instance: an event should have been fired, the transaction committed and so on. Use
AssertWasCalledfor this:Note: there is not return value, so there is no Stub. This is an ideal case. Of course, you could have both,
StubandAssertWasCalled.There is also
ExpectandVerifyAllExpectations, which actually behaves like the old syntax. I would only use them when you needStubandAssertWasCalledin the same test, and you have complext argument constraints which you don’t want to write twice. Normally, avoidExpectandVerifyAllExpectations