title: Moq Verify with anonymous type parameter
body: <p>I have the following test, with supporting classes, but I can't figure out how to verify the call on the dependency.</p>
  
  <pre><code>[TestFixture]
  public class AnonymousGenericTypeParameterTests
  {
      [Test]
      public void Test()
      {
          // Arrange
          var dependency = new Mock&lt;IDependency&gt;();
  
          var towns = new List&lt;Town&gt;
          {
              new Town { Name = "Lifford", County = "Donegal", Country="Ireland", Population = 1658 },
              new Town { Name = "Ballyshannon", County = "Donegal", Country="Ireland", Population = 2504 },
              new Town { Name = "Buxton", County = "Derbyshire", Country="United Kingdom", Population = 13599 },
          };
  
          var sut = new MyClass(dependency.Object);
  
          // Act
          sut.DoSomething(towns);
  
          // Assert
          // The following line needs to be fixed.
          dependency.Verify(d =&gt; d.Execute(It.IsAny&lt;IEnumerable&lt;object&gt;&gt;(), It.IsAny&lt;Func&lt;object, decimal?&gt;&gt;()));
      }
  }
  public interface IDependency
  {
      void Execute&lt;T&gt;(IEnumerable&lt;T&gt; collection, Func&lt;T, decimal?&gt; rateSelector);
  }
  public class MyClass
  {
      private readonly IDependency dependency;
      public MyClass(IDependency dependency)
      {
          this.dependency = dependency;
      }
      public void DoSomething(IEnumerable&lt;Town&gt; towns)
      {
          var counties = towns.GroupBy(t =&gt; new {t.Country,t.County});
          foreach (var county in counties)
          {
              dependency.Execute(county, c =&gt; c.Population);
          }
      }
  }
  public class Town
  {
      public string Name { get; set; }
      public string County { get; set; }
      public int Population { get; set; }
      public string Country { get; set; }
  }
  </code></pre>
  
  <p>According to Moq's test output, the performed invocations are:</p>
  
  <pre><code>Dependency.Execute(System.Linq.Lookup`2+Grouping[&lt;&gt;f__AnonymousType0`2[System.String,System.String],UniFocus.Staffscope4.Tests.Town], System.Func`2[UniFocus.Staffscope4.Tests.Town,System.Nullable`1[System.Decimal]])
  </code></pre>
  
  <p>I see plenty of questions regarding anonymous parameters in Moq (such as <a href="https://stackoverflow.com/questions/10299629/how-to-use-setup-of-a-mocked-anonymous-type">this</a> and <a href="https://stackoverflow.com/questions/9892362/using-moq-can-you-verify-a-method-call-with-an-anonymous-type">this</a> and <a href="https://stackoverflow.com/questions/12927118/mocking-a-method-with-an-anonymous-type-argument">this</a>), but can't find anything relating to using an anonymous type as the actual type parameter.</p>
  
  <p>What can be put in the Verify line so that it actually verifies the call inside?</p>
  
  <p><strong>Note:</strong> My example <code>IDependency</code> doesn't return a value (it's already complex enough, I think), but there will be bonus kudos for answers that implictly or explicitly address <code>Setup()</code> as well as <code>Verify()</code>.</p>
  
  <p><strong>Update</strong>
  Jesse's solution only passes the test because I made a bad choice when crafting my example. I should have realised that any <code>IGrouping&lt;out TKey, out TElement&gt;</code> is also an <code>IEnumerable&lt;TElement&gt;</code>. Is there a more universal solution?</p>
  
  <p><strong>Update 2</strong>
  I feel like my original example was possibly too elaborate and didn't represent well the actual title of my question. Is there any solution that works for this more straightforward and to-the-point example?</p>
  
  <pre><code>using Moq;
  using NUnit.Framework;
  
  namespace Tests
  {
      [TestFixture]
      public class SimpleAnonymousGenericTypeParameterTests
      {
          [Test]
          public void Test()
          {
              // Arrange
              var dependency = new Mock&lt;IDependency&gt;();
              var sut = new MyClass(dependency.Object);
  
              // Act
              sut.DoSomething("Donegal", "Lifford");
  
              // Assert
              // This verify works for both calls to Execute()
              dependency.Verify(d =&gt; d.Execute(It.IsAny&lt;object&gt;()), Times.Exactly(2));
              // This verify should specifically refer to only the first call to Execute()
              dependency.Verify(d =&gt; d.Execute(It.IsAny&lt;/*HowToRepresentAnonymousTypeHere*/object&gt;()), Times.Once);
          }
          public interface IDependency
          {
              void Execute&lt;T&gt;(T thing);
          }
          public class MyClass
          {
              private readonly IDependency dependency;
              public MyClass(IDependency dependency)
              {
                  this.dependency = dependency;
              }
              public void DoSomething(string county, string town)
              {
                  dependency.Execute(new { county, town });
                  object someUnknownObject = "";
                  dependency.Execute(someUnknownObject);
              }
          }
      }
  }
  </code></pre>