title: How to properly mock MongoDbClient
body: <h1>Context</h1>
  
  <p>I'm writing unit tests for an API I've been developing and I just ran into an issue while trying to UnitTest a "Context" for accessing a MongoDB Storage.</p>
  
  <p>I abstracted the current interface for my context:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface IProjectsContext
  {
      IMongoCollection&lt;Project&gt; Projects { get; }
  }
  </code></pre>
  
  <p>I'm able to successfully use this interface, together with <code>Moq</code> to UnitTest my Repositories.</p>
  
  <p>However, when trying to UnitTest my Context's implementation I've been unable to muster a solution for mocking the inwards:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ProjectsContext : IProjectsContext
  {
      private const string ProjectsCollectionName = "Projects";
  
      private readonly IDatabaseParameters _dbParams;
      private readonly MongoClient _client;
      private readonly IMongoDatabase _database;
  
      private IMongoCollection&lt;Project&gt; _projects;
  
      public ProjectsContext(IDatabaseParameters dbParams)
      {
          _dbParams = dbParams ?? throw new ArgumentNullException(nameof(dbParams));
          _client = new MongoClient(_dbParams.ConnectionString);
          _database = _client.GetDatabase(_dbParams.DatabaseName);
      }
  
      public IMongoCollection&lt;Project&gt; Projects
      {
          get
          {
              if (_projects is null)
                  _projects = _database.GetCollection&lt;Project&gt;(ProjectsCollectionName);
              return _projects;
          }
      }
  }
  </code></pre>
  
  <p>The unit test in question is:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private readonly Fixture _fixture = new Fixture();
  private readonly Mock&lt;IDatabaseParameters&gt; _dbParametersMock = new Mock&lt;IDatabaseParameters&gt;();
  
  public ProjectsContextTests()
  {
  
  }
  
  [Fact(DisplayName = "Create a Project Context")]
  public void CreateProjectContext()
  {
      // Arrange
      _dbParametersMock.Setup(m =&gt; m.ConnectionString).Returns(_fixture.Create&lt;string&gt;());
      _dbParametersMock.Setup(m =&gt; m.DatabaseName).Returns(_fixture.Create&lt;string&gt;());
  
      // Act
      var result = new ProjectsContext(_dbParametersMock.Object);
  
      // Assert
      result.Should().NotBeNull();
      result.Should().BeAssignableTo&lt;IProjectsContext&gt;();
      // TODO: Write a test to assert the ProjectCollection
  }
  </code></pre>
  
  <h1>Question</h1>
  
  <p>The only solution I can think of is changing my <code>ProjectsContext</code> to have a constructor with receives, as a parameter, the <code>IMongoDatabase</code> which is going to be used. However, is this the <em>only</em> solution?</p>
  
  <h1>Libraries used</h1>
  
  <p>I'm using the following NuGets for my UnitTests and Implementation:</p>
  
  <ul>
  <li>xUnit</li>
  <li>Coverlet.msbuild</li>
  <li>Moq</li>
  <li>AutoFixture</li>
  <li>FluentAssertions</li>
  <li>MongoDB</li>
  </ul>