Mock Cosmos Db-用于单元测试的Mongo Db API上下文

时间:2019-07-02 13:37:24

标签: unit-testing .net-core moq xunit.net

我试图通过模拟ContextComsDb来对Comos数据库-MongoDb API方法FindAll()进行单元测试。

public class ContextCosmosDb
{
    public virtual IMongoClient Client { get; set; }
    public virtual IMongoDatabase Database { get; set; }
    public ContextCosmosDb(IOptionsCosmosDb options)
    {
        var settings = MongoClientSettings.FromUrl(new 
        MongoUrl(options.MongoApiConnectionString));
        settings.SslSettings =
            new SslSettings { EnabledSslProtocols = SslProtocols.Tls12 };
        Client = new MongoClient(settings);

        Database = (Client != null) ? 
        Client.GetDatabase(options.DatabaseName) : null;
    }
}
public abstract class RepositoryBaseCosmosDb<T> : 
    IRepositoryBaseCosmosDb<T> where T : EntityBaseCosmosDb
{
    public RepositoryBaseCosmosDb(ContextCosmosDb cosmosDbContext)
    {
        RepositoryContext = cosmosDbContext;
    }
    public IQueryable<T> FindAll()
    {
        return RepositoryContext.Database.GetCollection<T> 
            (CollectionName).AsQueryable();
    }
}

下面是我为测试而编写的内容。

public class RepositoryBaseCosmosDbTest
{
    [Trait("Category", "FindAll")]
    [Fact]
    public void FindAll_ShouldReturnAllData()
    {
        // Arrange
        var mongoDatabase = new Mock<IMongoDatabase>();
        var collection = new Mock<IMongoCollection<CosmosDbBlog>>();
        var context = new Mock<ContextCosmosDb>
            (new OptionsCosmosDb<ContextCosmosDb> ("mongodb://localhost", "test"));
        collection.Object.InsertOne(new CosmosDbBlog { BlogId = 1, Name = "BBB" });
        collection.Object.InsertOne(new CosmosDbBlog { BlogId = 2, Name = "ZZZ" });
        collection.Object.InsertOne(new CosmosDbBlog { BlogId = 3, Name = "AAA" });
        context.Setup(x => x.Database.GetCollection<CosmosDbBlog> 
            ("CosmosDbBlogList", null)).Returns(collection.Object);
        var service = new BlogRepositoryCosmosDb<CosmosDbBlog>(context.Object);

        // Act
        var blogs = service.FindAll();

        // Assert
        Assert.True(blogs.Select(b => b.Name == "BBB").Any());
        Assert.True(blogs.Select(b => b.Name == "ZZZ").Any());
        Assert.True(blogs.Select(b => b.Name == "AAA").Any());
    }
}

我知道

  

System.NullReferenceException:'对象引用未设置为对象的实例'

当我断言时。如果可以的话请帮忙!

1 个答案:

答案 0 :(得分:0)

以下代码有效,

    [Trait("Category", "FindAll")]
    [Fact]
    public void FindAll_ShouldReturnAllData()
    {
        // Arrange
        var mongoDatabase = new Mock<IMongoDatabase>();
        var collection = new Mock<IMongoCollection<CosmosDbBlog>>();
        var context = new Mock<ContextCosmosDb>(new OptionsCosmosDb<ContextCosmosDb>("mongodb://localhost", "test"));
        context.Setup(x => x.Database.GetCollection<CosmosDbBlog>(It.IsAny<string>(), null))
            .Returns(collection.Object).Verifiable();

        // Act
        var service = new BlogRepositoryCosmosDb<CosmosDbBlog>(context.Object);
        var blogs = service.FindAll();

        // Assert
        context.Verify();
    }