Fake common FakeItEasy method

How are you going to pretend like this:

public interface IBlah
{
    Func<T, bool> ApplyFilter<T>(Func<T, bool> predicate) where T:IMessage;
}

I would like the fake to simply return the argument without any change. However, I would like to verify that the fake was called exactly once. An example of use is given below:

  public class Something
  {

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

     public bool DoSomething(SomeValue m, Func<SomeValue, bool> predicate)
     {
         Func<SomeValue, bool> handler = _blah.ApplyFilter(predicate);
         return handler(m);
     }
  }

i.e. the fake should act like a passage, but I should also be able to verify that it was used.

What is the best way to do this?

[Please don't worry about a far-fetched example ... there are a lot of things going on under the covers, but I simplified it to the example above.]

+5
source share
1 answer

Will this solve your problem? It will go through the predicate and also check that ApplyFilter is called exactly once

    [Fact]
    public void TestFeature()
    {
        var fake = A.Fake<IBlah>();
        A.CallTo(() => fake.ApplyFilter(A<Func<int, bool>>.Ignored)).ReturnsLazily(x =>
            {
                return x.GetArgument<Func<int, bool>>("predicate");
            });
        var something = new Something(fake);
        var result = something.DoSomething(1, x => x > 1);

        Assert.False(result);
        A.CallTo(() => fake.ApplyFilter(A<Func<int, bool>>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
    }
+2
source

All Articles