Using Moq to verify in C#
<p>I'm trying to mock a database so that I can verify that the save method was called. I have a project which saves to a database, it takes a list of the objects to save, and a connection string.</p>

<pre><code>this._database.Save&lt;Constraint&gt;(constraints, "DEFAULT");
</code></pre>

<p>When I debug I can see that the test successfully goes into my project with a mocked database, and that it hits the save line exactly once.</p>

<p>In my test project I create an instance of the class calling the save method, create and create a mock database, and use .Setup for the save method.</p>

<pre><code>private Mock&lt;IDatabase&gt; _mockDatabase;
...
_mockDatabase = new Mock&lt;IDatabase&gt;();
_mockDatabase.Setup(d =&gt; d.Save&lt;Types.Constraint&gt;(It.IsAny&lt;Types.Constraint&gt;(), It.IsAny&lt;String&gt;()));
</code></pre>

<p>Then in my test method, I call .Verify to make sure that save was called one time.</p>

<pre><code>_mockDatabase.Verify(d =&gt; d.Save&lt;Constraint&gt;(It.IsAny&lt;Constraint&gt;(), It.IsAny&lt;String&gt;()), Times.Once);
</code></pre>

<p>However the test fails on this verify. Does anyone know how I can fix this? Thanks for any help/ideas!</p>

<blockquote>
  <p>Moq.MockException:<br>
  Expected invocation on the mock once, but was 0 times: d => d.Save(It.IsAny(), It.IsAny())</p>
  
  <p>Configured setups:<br>
  d => d.Save(It.IsAny(), It.IsAny()), Times.Never</p>
  
  <p>Performed invocations:<br>
  IDatabase.Save(System.Collections.Generic.List`1[Types.Constraint], "DEFAULT")</p>
</blockquote>