I have an extension method with the following signature:
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
...
}
I have written a test-case for it that makes sure the two expressions are in fact combined. At least so that the new expression I get works.
Now I would like to write another test-case that just makes sure that the method uses the short-circuiting version of and
. Any clue how I can do this?
I thought I could just do something like this:
[Test]
public void And_PredicatesAreShortCircuited()
{
var predicateNotUsed = true;
Expression<Func<int, bool>> a = x => false;
Expression<Func<int, bool>> b = x =>
{
predicateNotUsed = false;
return true;
};
var foo = new[] { 1, 2, 3, 4, 5, 6, 7 }
.Where(a.And(b).Compile())
.ToArray();
Assert.That(predicateNotUsed);
}
But I get a giant red squiggly under that whole statement body for b
stating that "A lambda expression with a statement body cannot be converted to an expression tree". So... any options? Or is this an impossible test to write?