title: How would I use moq to test a MongoDB service layer?
body: <p>I have a service layer between my app and the mongo database.</p>
  
  <p>I'm trying to build a unit test using moq 
  I'm quite new to moq so I started with what I thought would be a trivial test.</p>
  
  <p>Code to test:</p>
  
  <pre><code>    public List&lt;BsonDocument&gt; GetAllSettings()
      {
          var collection = MongoDatabase.GetCollection&lt;BsonDocument&gt;("Settings");
          var query = from e in collection.AsQueryable()
                      select e;
  
          var settings = query.ToList();
          return settings;
      }
  </code></pre>
  
  <p>Where:
  Settings is a Collection 
  MongoDatabase is a MongoDBDriver.MongoDatabase</p>
  
  <p>I've tried this as my test:</p>
  
  <pre><code>    [Test()]
      public void GetAllSettingsTest()
      {
          //Arrange
          BsonDocument doc01 = new BsonDocument();
          BsonDocument doc02 = new BsonDocument();
  
          var mongoDatabase = new Mock&lt;MongoDatabase&gt;();
          var collection = new Mock&lt;MongoCollection&lt;BsonDocument&gt;&gt;();
          mongoDatabase.Setup(f =&gt; f.GetCollection(MongoCollection.Settings)).Returns(collection.Object);
          collection.Object.Insert(doc01);
          collection.Object.Insert(doc02);
  
          ILogger logger = new Logger();
          DatabaseClient.DatabaseClient target = new DatabaseClient.DatabaseClient(logger);
          target.MongoDatabase = mongoDatabase.Object;
  
          MongoCursor&lt;BsonDocument&gt; cursor = collection.Object.FindAllAs&lt;BsonDocument&gt;();
  
          List&lt;BsonDocument&gt; expected = cursor.ToList();
          List&lt;BsonDocument&gt; actual;
  
          //Act
          actual = target.GetAllSettings();
  
          //Assert
          Assert.AreEqual(expected, actual);
      }
  </code></pre>
  
  <p>I'm getting an error of "Could not find a parameterless constructor" at:</p>
  
  <p>mongoDatabase.Setup(f => f.GetCollection(MongoCollections.Settings)).Returns(collection.Object);</p>
  
  <p>The error refers to the MongoCollection object.  I didn't think it had a constructor.</p>
  
  <p>What can I do to get my test to run?</p>