1

I'm trying to do something like this, and it doesn't work, although it kinda should be

A.CallTo(() => partyRepo.Where(o => o.Person != null))
                        .Returns(new[] {new Party()});

the call to this method with this exact code as a parameter returns an empty Enumerable

Omu
  • 69,856
  • 92
  • 277
  • 407

1 Answers1

3

The reason is that the expression you're passing in the call specification and the one that's actually sent to the partyRepo will not be equal. There's no way for FakeItEasy to determine if the arguments are the but to use the Equals-method.

I'm not really sure what would be the best workaround, you could do something like this:

public static class MyArgumentConstraints
{
    public static Func<MyClass, bool> ReturnsTrueWhenPersonIsNotNull(this IArgumentConstraintManager<Func<MyClass, bool>> manager)
    {
        return manager.NullCheckedMatches(x =>
                                                {
                                                    return x.Invoke(new MyClass() {Person = "person"}) &&
                                                            !x.Invoke(new MyClass() {Person = null});
                                                },
                                            x => x.Write("predicate that returns true for non null person"));
    }
}

public class MyClass
{
    public string Person { get; set; }
}

With this extension you can now rewrite your original line:

A.CallTo(() => partyRepo.Where(A<Func<MyClass, bool>>.That.ReturnsTrueWhenPersonIsNotNull())
                    .Returns(new[] {new Party()});
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60