Method calls in Rhino Mocks can be ordered or unordered. The default state for a recorder is unordered recording, this means that during replay, methods can come at any order. If the recorder is changed to ordered, then the methods must be called in the exact same order as they were recorded. Here is a code sample:
public void SaveProjectAs_NewNameWithoutConflicts()
projectView.Title = newProjectName;
projectView.HasChanges = false;
In the above code example we ask Rhino Mocks to verify that the calls come in the exact same order. Notice that we specify expectations on several mock objects, and Rhino Mocks will handle the ordering between them. This mean that if I set the project view title before I get a project from the repository, the test will fail. In Rhino Mocks, the default is to use unordered matching, but it supports unlimited depth of nesting between ordered and unordered. This means that you can create really powerful expectations. Here is a somewhat contrived example:
public void MovingFundsUsingTransactions()
MockRepository mocks = new MockRepository();
IDatabaseManager databaseManager = mocks.StrictMock<IDatabaseManager>();
IBankAccount accountOne = mocks.StrictMock<IBackAccount>(),
accountTwo = mocks.StrictMock<IBackAccount>();
Bank bank = new Bank(databaseManager);
This code verifies that the transfer of funds from one account to another is wrapped in a transaction, but the implementation is free to withdraw from the first account first, or to deposit into the second account first, both are legal, as long as both actions happens. The reverse is true as well, you may specified unordered sequence of ordered events (I want A then B then C to happen, and I want D then E then F to happen, but I don't care which sequence comes first).Ordering has two caveats:
- To exit an ordering in replay state, you must call all the recorded methods. In the above example, we can move from the inner Unordered ordering (the one containing the withdraw and deposit) only after both methods were called. This falls in line with the way the recording code looks, so it shouldn't cause any surprises.
- You must exit all ordering before you can start replaying. This is enforced by the framework (you get an exception if you try).
Up: Rhino Mocks Documentation
Next: Rhino Mocks Mocking Delegates