Moq Expression with Constraint ... It.Is<Expression<Func<T, bool>>>
<p>Ok, I am having a hard time trying to figure out how to setup a moq for a method that takes in an expression. There are a lot of examples out there of how to to It.IsAny&lt;>... that is not what I am after. I am after doing with a constraint, so It.Is&lt;>. I have set it up but it never returns the value I have asked it to return. </p>

<pre><code>// Expression being setup
Expression&lt;Func&lt;UserBinding, bool&gt;&gt; testExpression = binding =&gt;
binding.User.Username == "Testing Framework";


// Setup of what expression to look for. 
 this.bindingManager.Setup(
            c =&gt; c.GetUserBinding(It.Is&lt;Expression&lt;Func&lt;UserBinding, bool&gt;&gt;&gt;
(criteria =&gt; criteria == testExpression)))
            .Returns(new List&lt;UserBinding&gt;() { testBinding }.AsQueryable());

// Line of code call from within the class being tested. So this is being mocked and should return the defined object when the same lambda is passed in.
 this.bindingManager.GetUserBinding(b =&gt; b.User.Username == username)
                .SingleOrDefault();

// class under test. So for the test this is being called. 
// so this is the method being called and inside this method is where the binding manager is being mocked and called. 
var response = this.controller.SendMessage(message, true).Result;

        response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

 // inside the controller.SendMessage method this method is called with the lambda expression. I have verified the usernames match but when the setup is It.Is this returns null instead of the object setup in the "setup" call. 
this.bindingManager.GetUserBinding(b =&gt; b.User.Username == username)
                .SingleOrDefault();
</code></pre>

<p>If I change the setup to It.IsAny... It works and returns the expected object setup in the "returns" method. </p>

<p>I have found a few examples of how to do this on the web one is doing it this way the other is using compile but I can't get that to work either. How do you get this to work for a specific expression? </p>

<h2>Update with working solution based on answer</h2>

<hr>

<p>@carlos-alejo got me going in the right direction or at least kicked me back to the Compile action. I was thinking about it wrong. I have the solution working now based on using compile. The key thing to understand about compile is you are giving it an object by which to evaluate/generate the expression for. </p>

<p>So in my case if some one is giving me an expression like this:</p>

<pre><code>binding =&gt; binding.User.Username == "Testing Framework";
</code></pre>

<p>I need to have a UserBinding like this:</p>

<pre><code>var testBinding = new UserBinding { Binding = new Binding { Name = "Default binding" }, Domain = "test.com", User = new User() { Username = "Testing Framework" } };
</code></pre>

<p>I can then create my "setup" call like this:</p>

<pre><code>this.bindingManager.Setup(c =&gt; c.GetUserBinding(It.Is&lt;Expression&lt;Func&lt;UserBinding, bool&gt;&gt;&gt;(y =&gt; y.Compile()(testBinding))))
        .Returns(new List&lt;UserBinding&gt;() { testBinding }.AsQueryable());
</code></pre>

<p>This works and in my case returns me back the test binding object as I have setup. If you change the testBinding to be <strong>(notice I changed the user name)</strong>:</p>

<pre><code>    var testBinding = new UserBinding { Binding = new Binding { Name = "Default binding" }, Domain = "test.com", User = new User() { Username = "Testing Framework2" } };
</code></pre>

<p>It will not work because the code inside my system under test generates an expression looking for "Test Framework"</p>

<p>Maybe it was just me not connecting the dots on this but hopefully it helps others. </p>