title: How to verify a method was called if it returns no value?
body: <p>I'm trying to prepare a mock object using Moq to test a method (<code>SaveNewPersonAsync()</code>) which calls a LogMessage method in my Logger object. <code>SaveNewPersonAsync()</code> does not return any value (well, except the Task&lt;&gt; as it's async) but, as I understood from Moq tutorials, I'm supposed to verify a method's returned value. And also, the mock.Verify method always passes for me, even if I don't call the <code>SaveNewPersonAsync()</code>. How can I verify that <code>SaveNewPersonAsync()</code> and <code>LogMessage()</code> inside of it were called?</p>
  <pre><code>// Create the mock
  var mock = new Mock&lt;ILogger&gt;();
  
  // Configure the mock to do something
  mock.Setup(x =&gt; x.LogMessage(&quot;test&quot;)).Returns(true);
  
  // Demonstrate that the configuration works
  Assert.AreEqual(true, mock.Object.LogMessage(&quot;test&quot;));
  
  
  // perform the db saving here
  var dataproc = new DataProcessor(mock.Object, new UserDbMongo(), new ParsingManager());
  await dataproc.SaveNewPersonAsync(GenerateJsonData(&quot;CocaColaParser&quot;));
  
  
  mock.Verify(x =&gt; x.LogMessage(&quot;test&quot;));
  // verify() passes even if I don't call the SaveNewPersonAsync() method
  </code></pre>
  <p>The <code>SaveNewPersonAsync()</code> method, simplified:</p>
  <pre><code>await _dbmanager.InsertUserAsync(person);
  _logger.LogMessage(&quot;Saved a user to db.&quot;); // if this is not here, the test should fail
  </code></pre>