title: How do I verify mocked async method called with correct expression?
body: <p>I have written some tests using XUnit and Moq. One method of the mocked interface implementation is accepting a parameter of type <code>Expression&lt;Func&lt;T, bool&gt;&gt;</code>  Everything seems to be working well but I have trouble understanding the working of verifying that the method is called with the right expression.</p>
  
  <p>Given the following test, the method does not return the value specified in the setup, even though the call seems correct.:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Verify that a create fails appropriately when another entity was found given the same name,
      /// Verify that the message of the exception contains the duplicate name
      /// Verify that update fails before other calls are made
      /// &lt;/summary&gt;
      [Theory(DisplayName = "Definition Types service - Create")]
      [MemberData(nameof(DefinitionTypesTestData.SingleDefinitionType), MemberType = typeof(DefinitionTypesTestData))]
      public async Task CreateDefinitionTypeShouldThrowDuplicateTest(DefinitionType obj)
      {
          if (obj == null) throw new NullReferenceException($"Test data was null in theory 'Definition Types service - Create'");
          var crudService = new Mock&lt;IEntityCrudService&lt;DefinitionType&gt;&gt;();
          crudService.Setup(crud =&gt; crud.GetEntitiesAsync(x =&gt; x.Name == obj.Name))
              .Returns(Task.FromResult&lt;IEnumerable&lt;DefinitionType&gt;&gt;(new List&lt;DefinitionType&gt; {
              new DefinitionType {
                  Name = "Test",
                  Description = "Test",
                  DisplayName = "Test",
                  ID = Guid.NewGuid()
              } }));
          IDefinitionTypesService serviceUnderTest = new DefinitionTypesService(crudService.Object);
          var exception = await Assert.ThrowsAsync&lt;EntityDuplicationException&gt;(() =&gt; serviceUnderTest.InsertDefinitionTypeAsync(obj));
          Assert.Contains("Definition type", exception.DisplayMessage);
          Assert.Contains(obj.Name, exception.DisplayMessage);
          crudService.Verify(crud =&gt; crud.GetEntitiesAsync(x =&gt; x.Name == obj.Name), Times.Once);
          crudService.VerifyNoOtherCalls();
      }
  </code></pre>
  
  <p>I have the following implementation for InsertDefinitionType(DefinitionType obj):</p>
  
  <pre><code>async Task IDefinitionTypesService.InsertDefinitionTypeAsync(DefinitionType obj)
      {
          var definitiontypes = await _definitionTypeService.GetEntitiesAsync(x =&gt; x.Name == obj.Name);
  
          if(definitiontypes.Any())
          {
              throw new EntityDuplicationException("Definition type", name: obj.Name);
          }
  
          try
          {
              await _definitionTypeService.CreateAsync(obj);
          }
          catch (EntityNotSavedException exc)
          {
              exc.EntityType = "Definition type";
              throw exc;
          }
      }
  </code></pre>
  
  <p>When I change my setup as follows I do get the result, but in my verify function it says the function is never called (or at least with the given expression). :</p>
  
  <pre><code>crudService.Setup(crud =&gt; crud.GetEntitiesAsync(It.IsAny&lt;Expression&lt;Func&lt;DefinitionType, bool&gt;&gt;&gt;()))
      .Returns(Task.FromResult&lt;IEnumerable&lt;DefinitionType&gt;&gt;(new List&lt;DefinitionType&gt; {
          new DefinitionType {
              Name = "Test",
              Description = "Test",
              DisplayName = "Test",
              ID = Guid.NewGuid()
          } }));
  </code></pre>
  
  <p>Right now I have also changed the verification to be more generic:</p>
  
  <pre><code>crudService.Verify(crud =&gt; crud.GetEntitiesAsync(It.IsAny&lt;Expression&lt;Func&lt;DefinitionType, bool&gt;&gt;&gt;()), Times.Once);
  </code></pre>
  
  <p>Now my test passes, but I really want to verify the method is called correctly instead of being called at all. How do I solve this the easiest / best way?</p>