1

As I understand, ordered assertions in FakeItEasy 2 are done like this (from the docs):

// Assert
A.CallTo(() => unitOfWorkFactory.BeginWork()).MustHaveHappened()
    .Then(A.CallTo(() => usefulCollaborator.JustDoIt()).MustHaveHappened())
    .Then(A.CallTo(() => unitOfWork.Dispose()).MustHaveHappened());

Now, suppose I have a collection and for each item in this collection I want to assert that a call was made to a faked object. What is the best approach to assert the calls were made in the correct order?

I came up with this, but don't really like it:

    IOrderableCallAssertion ioca = null;
    foreach (var item in items.OrderBy(i => i.Id)
    {
        var itemUnderTest = item;
        if (ioca == null)
        {
            ioca = A.CallTo(() => fakeObject.Handle(itemUnderTest, otherArgument)).MustHaveHappened(Repeated.Exactly.Once);
        }
        else
        {
            ioca = ioca.Then(A.CallTo(() => fakeObject.Handle(itemUnderTest, otherArgument)).MustHaveHappened(Repeated.Exactly.Once));
        }
    }
AroglDarthu
  • 1,021
  • 8
  • 17

1 Answers1

0

That looks about right to me. Of course, you could inline itemUnderTest and pull MustHaveHappened outside of the two if branches.

And you could always hide this in a convenience method.

An alternative: use Invokes to capture the fakes as the calls come in and later compare them against a list.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111