title: NSubstitute ILogger .NET Core
body: <p>I am trying to write unit tests around my exception handling so that I can verify the my logger is properly logging the exception. I am using NSubstitute as a mocking framework and <code>Microsoft.Extensions.Logging.ILogger</code> I have to following for my test:</p>
  
  <pre><code>[Fact]
  public void LogsExcpetionWhenErrorOccursInCreate()
  {
     var newUser = new UserDataModel
     {
        FirstName = "Rick",
        MiddleName = "Jason",
        LastName = "Grimes",
        Email = "rick.grimes@thedead.com",
        Created = new DateTime(2007, 8, 15)
     };
     var exception = new Exception("Test Exception");
     // configure InsertOne to throw a generic excpetion
     _mongoContext.InsertOne(newUser).Returns(x =&gt; { throw exception; });
  
     try
     {
        _collection.Create(newUser);
     }
     catch
     {
        // validate that the logger logs the exception as an error
        _logger.Received().LogError(exception.Message);
     }
  }
  </code></pre>
  
  <p>to test the logging in the following method:</p>
  
  <pre><code>public UserDataModel Create(UserDataModel user)
  {
       try
       {
            return MongoContext.InsertOne(user);                
       }
       catch(Exception e)
       {
             _logger?.LogError(e.Message);
             throw new DataAccessException("An error occurred while attempting to create a user.", e);
        }
  </code></pre>
  
  <p>}</p>
  
  <p>My test fails with the following error:</p>
  
  <pre><code>Message: NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
      Log&lt;Object&gt;(Error, 0, Test Exception, &lt;null&gt;, Func&lt;Object, Exception, String&gt;)
  Actually received no matching calls.
  Received 1 non-matching call (non-matching arguments indicated with '*' characters):
      Log&lt;Object&gt;(Error, 0, *Test Exception*, &lt;null&gt;, Func&lt;Object, Exception, String&gt;)
  </code></pre>
  
  <p>I'm not sure why this is failing because even in then error message the calls are the same.</p>
  
  <p>Thanks in advance! </p>
  
  <p>Update:</p>
  
  <p>Here is the constructor for the test, this is where I am injecting the logger mock:</p>
  
  <pre><code>public UserCollectionTest()
  {
     _mongoContext = Substitute.For&lt;IMongoContext&lt;UserDataModel&gt;&gt;();
     _logger = Substitute.For&lt;ILogger&gt;();
     // create UserCollection with our mock client
     _collection = new UserCollection(_mongoContext, _logger);
  }
  </code></pre>