How to Mock IMongoDatabase

Viewed 2852

I'm using Moq for mocking my objects in an ASP.NET Core project.

I want to mock the following IsConnection() method:

public Client(IMongoClient client)
{
    _client = client;
}

public async Task<bool> IsConectionOk()
{            
    var pingCommand = new BsonDocument("ping", 1);
    var mongoDb = _client.GetDatabase("Name");
    var commandResult = await mongoDb.RunCommandAsync<BsonDocument>(pingCommand);
    return commandResult != null;        
}

As you see, there's only one injection, IMongoClient, so I need to mock this one. Now, I need to mock IMongoDatabase as well since the _client.GetDatabase returns me an IMongoDatabase which runs RunCommandAsync

this is my unit test:

[Fact]
public async Task IsConnectionOk_xxx_RunPing1Command()
{
    var dbMock = new Mock<IMongoDatabase>();
    var resultCommand = new BsonDocument("ok", 1);
    dbMock.Setup(stub => stub.RunCommandAsync<BsonDocument>(It.IsAny<BsonDocument>(), It.IsAny<ReadPreference>(), It.IsAny<CancellationToken>())).ReturnsAsync(resultCommand);

    var mongoClientMock = new Mock<IMongoClient>();
    mongoClientMock.Setup(stub => stub.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>())).Returns(dbMock.Object);

    var client = new Client(mongoClientMock.Object);
    var pingCommand = new BsonDocument("ping", 1);

    //act
    await client.IsConectionOk();

    //assert
    dbMock.Verify(mock => mock.RunCommandAsync<BsonDocument>( It.Is<BsonDocument>(x => x.Equals(pingCommand)) , It.IsAny<ReadPreference>() ,It.IsAny<CancellationToken>()));
}

You can see that I mocked a IMongoDatabase so my mongoClientMock can return it when the code is executing. When code is running, I have checked that mongoClientMock.GetDatabase() is returning a MongoDatabase (good until there), the problem is that when MongoDatabaseMock calls RunCommandAsync is not returning what I set up, it just returns null. I don't know what I could be missing here, any thoughts?

2 Answers
Related