How to test whether a Func<T> has been called using the FakeItEasy framework?

c#, fakeiteasy, func

Solution

You can do this assuming you are providing the `Func` to the code under test. You just make a `Fake` out of it like you would any other type. It will look something like this.

public class Foo {
  public bool Bar(Func<bool> fn) {
    return fn();
  }
}

[Test]
public void Should_call_fn() {
  var fn = A.Fake<Func<bool>>();

  (new Foo()).Bar(fn);

  A.CallTo(() => fn.Invoke()).MustHaveHappened();
}

Problem

How to check whether a Func has been called using the FakeItEasy framework? Example: ``` Func<bool> myFunc = () => true; // Unfortunately this fails: A.CallTo(myFunc.Invoke()).MustHaveHappened(); ```

Original source