How you assert through unit test that an user is authenticated before doing withdraw operation? You can surely verify a method is invoked as expected but if you want to ensure the order right then you might require a little more. JustMock lets you specify the order in which your setups should be executed. This helps you identify the exact way in which a particular logic is implemented.
To begin, lets consider the following context:
User wants to withdraw money from his account. Withdraw operation should validate the following goals:
Now we have one IUserService interface
One IAccountService interface to process the accounts operation:
The basic AccountRepository class with minimal implementation covering the above context looks like:
Ensuring every step to be executed in an orderly manner , we just need to specify an extra InOrder option in Mock.Arrange that will otherwise fail the test during assert for any change of the expected execution order.
Let’s remove the line# 13 from AccountRepository.Withdraw that yields:
Since we broke the order, the test will fail with the following message:
Here one thing to notice that InOrder is applied to different mock instances within the test method scope that makes it effective in most practical and wide variety of scenarios. I have used Q3 SP build for the purpose (Also available via NuGet).
Hope that helps