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 wrote a test case for it, which ensures that both expressions are actually combined. At least so that the new expression I get works.
Now, I would like to write another test case that simply ensures that the method uses a short-circuited version of and . Any clue how can I 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 this whole body for b , stating that "a lambda expression with an operator body cannot be converted to an expression tree." So ... any options? Or is it an impossible test to write?
source share