Just want to confirm the idea :) I check out the RhinoMocks. It turn
out to be quite straightforward, thanks to nice design of it, I
created a class called CallRecord which keeps a thread safe sequencer
that is increased for each call and later use that information to
determine the order of each call.
public class CallRecord
{
private static long sequencer = long.MinValue;
internal CallRecord()
{
Sequence = Interlocked.Increment(ref sequencer);
}
internal object[] Arguments { get; set; }
internal long Sequence { get; private set; }
internal MethodInfo Method { get; set; }
internal IMockedObject MockObject { get; set; }
}
Then make AssertWasCalled methods return an IList<CallRecord>. And
added an extension method:
public static void Before(this IList<CallRecord> beforeCalls,
IList<CallRecord> afterCalls)
Now you can do this to verify the order:
mock1.AssertWasCalled(m1=>m1.MethodBefore())
.Before(mock2.AssertWasCalled(m2=>MethodAfter());
I have attached the patch. And there is the unit test which is also
included in the patch.
Cheers,
Kenneth
[TestFixture] public class BeforeExtensionMethodTest
{
public interface IBefore
{
void MethodBefore();
}
public interface IAfter
{
void MethodAfter();
}
[Test] public void
Before_succeeds_if_beforeCalls_occured_before_afterCalls()
{
var mockBefore = MockRepository.GenerateStub<IBefore>();
var mockAfter = MockRepository.GenerateStub<IAfter>();
mockBefore.MethodBefore();
mockBefore.MethodBefore();
mockAfter.MethodAfter();
mockAfter.MethodAfter();
mockAfter.MethodAfter();
mockBefore.AssertWasCalled(b => b.MethodBefore())
.Before(mockAfter.AssertWasCalled(a => a.MethodAfter()));
}
[ExpectedException(typeof(ExpectationViolationException))]
[Test] public void
Before_chokes_if_one_of_beforeCalls_occured_after_any_of_afterCalls()
{
var mockBefore = MockRepository.GenerateStub<IBefore>();
var mockAfter = MockRepository.GenerateStub<IAfter>();
mockBefore.MethodBefore();
mockAfter.MethodAfter();
mockBefore.MethodBefore();
mockAfter.MethodAfter();
mockAfter.MethodAfter();
mockBefore.AssertWasCalled(b => b.MethodBefore())
.Before(mockAfter.AssertWasCalled(a => a.MethodAfter()));