使用Moq的单元测试cosmosDb方法 [英] Unit test cosmosDb methods using Moq

查看:51
本文介绍了使用Moq的单元测试cosmosDb方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为没有测试CosmosDb的文档,所以我想自己做,但是这样做很麻烦.例如,我想测试一个如下所示的插入方法:

Since there is no documentation for testing CosmosDb I am trying to do it myself but I have trouble doing it. For example I want to test an insert method that looks like this:

public async Task AddSignalRConnectionAsync(ConnectionData connection)
{
    if (connection != null)
    {
        await this.container.CreateItemAsync<ConnectionData>(connection, new PartitionKey(connection.ConnectionId));
    }
}

我需要做的是测试此方法是否成功在cosmosDb上创建了一个项目,或者至少伪造了成功的创建.我该如何测试?

What I need to do is test if this method successfully create an item on the cosmosDb or at least fakes an successful creation. How can I test this?

推荐答案

为了单独对该方法进行单元测试,需要模拟被测类的依赖项.

In order to unit test that method in isolation, the dependencies of the class under test would need to be mocked.

假设像下面的例子

public class MySubjectClass {
    private readonly Container container;

    public MySubjectClass(Container container) {
        this.container = container;
    }

    public async Task AddSignalRConnectionAsync(ConnectionData connection) {
        if (connection != null) {
            var partisionKey = new PartitionKey(connection.ConnectionId);
            await this.container.CreateItemAsync<ConnectionData>(connection, partisionKey);
        }
    }        
}

在上面的示例中,被测方法取决于 Container ConnectionData ,它们在测试时需要提供.

In the above example, the method under test is dependent on Container and ConnectionData, which need to be provided when testing.

除非您想点击 Container 的实际实例,否则建议您模拟依赖项,如果使用实际的实现,这些依赖项可能具有不良行为.

Unless you want to hit an actual instance of the Container it would be recommended to mock dependencies that can have undesirable behavior if an actual implementation is used.

public async Task Should_CreateItemAsync_When_ConnectionData_NotNull() {
    //Arrange
    //to be returned by the called mock
    var responseMock = new Mock<ItemResponse<ConnectionData>>();

    //data to be passed to the method under test
    ConnectionData data = new ConnectionData {
        ConnectionId = "some value here"
    };

    var containerMock = new Mock<Container>();
    //set the mock expected behavior
    containerMock
        .Setup(_ => _.CreateItemAsync<ConnectionData>(
            data, 
            It.IsAny<PartitionKey>(), 
            It.IsAny<ItemRequestOptions>(),
            It.IsAny<CancellationToken())
        )
        .ReturnsAsync(responseMock.Object)
        .Verifiable();

    var subject = new MySubjectClass(containerMock.Object);

    //Act
    await subject.AddSignalRConnectionAsync(data);

    //Assert
    containerMock.Verify(); //verify expected behavior
}

根据上述隔离的单元测试示例,可以验证被测对象方法在参数不为null时将调用预期方法.

The subject method under test can be verified that it will call the expected method when the parameter is not null based on the above isolated unit test example.

使用真实的 Container 将使它成为集成测试,这将需要不同类型的测试安排.

Using a real Container would make this an integration test, which would require a different kind of test arrangement.

您还可以在此处检查开发人员如何对SDK进行测试

You could also check how the developers unit test the SDK here

信用: @MatiasQuaranta

这篇关于使用Moq的单元测试cosmosDb方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆