How to make a choice of calls with FakeItEasy

advertisements

With FakeItEasy, how to assert, that any of calls has happened?

The use case is I'm writing a class that works with a repository and as a result of a method, the class should remove some elements from the repository, either by calling DeleteAll, or by calling Delete for all of them.

For now, I've used a try-catch like this:

try // either
{
    A.CallTo(() => module.CardRepo.Delete(A<CardData>.That.IsEqualTo(dummy.CardData[0]))).MustHaveHappened();
    A.CallTo(() => module.CardRepo.Delete(A<CardData>.That.IsEqualTo(dummy.CardData[1]))).MustHaveHappened();
}
catch (ExpectationException) // or
{
    A.CallTo(() => module.CardRepo.DeleteAll(A<IEnumerable<CardData>>.That.Contains(dummy.CardData[0]))).MustHaveHappened();
    A.CallTo(() => module.CardRepo.DeleteAll(A<IEnumerable<CardData>>.That.Contains(dummy.CardData[1]))).MustHaveHappened();
}

but I don't like that and for more choices it would quickly become really ugly. Is there a better way? I couldn't find anything on FakeItEasy wiki.


Here's an example of how you can verify if one of more methods were called on a fake object.

 public interface IBlah
{
    void Delete(int x);
    int Add(int x, int y);
    int Subtract(int x, int y);
}

public class Something
{
    private readonly IBlah _blah;

    public Something(IBlah blah) { _blah = blah; }

    public void DoSomething(int x, int y )
    {
      //  _blah.Add(x, y);
        _blah.Subtract(x, y);
    }
}

Above is just a trivial system to test

   [Fact]
    public void TestFeature()
    {
        var fake = A.Fake<IBlah>();
        var something = new Something(fake);

        something.DoSomething(1, 2);

        var callResults = Fake.GetCalls(fake).Any(call =>
         {
             var x = call.GetArgument<int>("x");
             var y = call.GetArgument<int>("y");
             return call.Method.Name == "Add" || call.Method.Name == "Subtract"
                 && x == 1 && y == 2;
         });

        Assert.True(callResults);
    }

Above immediate snippet above is a XUnit test that verifies if either Add or Subtract were called with the giving values for the parameters x =1, y = 2