如何在Nest中测试异常?

时间:2018-11-09 19:40:19

标签: c# mstest nest fakeiteasy

当Nest在IGetResponse.OriginalException属性中具有值时,我正在尝试测试某些异常的结果。

我首先设置响应:

var response = A.Fake<Nest.IGetResponse<Dictionary<string, object>>>();
A.CallTo(() => response.OriginalException).Returns(new Exception("Status code 404"));

然后是假的弹性客户端:

var client = A.Fake<Nest.IElasticClient>();
A.CallTo(client)
    .WithReturnType<Nest.IGetResponse<Dictionary<string, object>>>()
    .Returns(response);

客户端被注入到我正在测试的类中。

但是,单步执行代码时,调用客户端时,它将返回伪造的响应,但是OriginalException吸气剂没有任何价值。它不为null,但所有属性都不具有任何值。我期望OriginalException.Message等于状态代码404

我还尝试将响应对象设置为:

var response = A.Fake<Nest.IGetResponse<Dictionary<string, object>>>();
A.CallTo(() => response.OriginalException.Message).Returns("Status code 404");

...效果同样差。

如何设置IGetResponse,以便可以在测试的类中评估OriginalException.Message

请求更多代码。我可以显示整个测试,并显示正在测试的方法。这是我的整个测试:

    [TestMethod]
    [ExpectedException(typeof(NotFoundException))]
    public void Get_ClientReturns404_ThrowsNotFoundException()
    {
        // setup
        var request = new DataGetRequest
        {
            CollectionName = string.Empty,
            DocumentType = string.Empty,
            DataAccessType = string.Empty
        };

        var response = A.Fake<Nest.IGetResponse<Dictionary<string, object>>>();
        A.CallTo(() => response.OriginalException.Message).Returns("Status code 404");

        var client = A.Fake<Nest.IElasticClient>();
        A.CallTo(client)
            .WithReturnType<Nest.IGetResponse<Dictionary<string, object>>>()
            .Returns(response);

        var elasticSearch = new ElasticSearch(null, client);

        // test
        var result = elasticSearch.Get(request);

        // assert
        Assert.Fail("Should have hit an exception.");
    }
}

这是正在测试的方法:

    public async Task<Dictionary<string, object>> Get(DataGetRequest getRequest)
    {
        GetRequest request = new GetRequest(getRequest.CollectionName, getRequest.DocumentType, getRequest.Id);
        var response = await Client.GetAsync<Dictionary<string, object>>(request);

        if (response.OriginalException != null)
        {
            var message = response.OriginalException.Message;
            if (message.Contains("Status code 404"))
                throw new NotFoundException(String.Format("Not Found for id {0}", getRequest.Id));
            else
                throw new Exception(message);
        }                

        return response.Source;
    }

IF块中的错误处理不是很可靠。一旦单元测试成功,那么该代码可能会收到更多的爱。

1 个答案:

答案 0 :(得分:2)

模拟的客户端的返回类型错误,因为IElasticClient.GetAsync<>返回了Task<IGetResponse<T>>

Task<IGetResponse<T>> GetAsync<T>(IGetRequest request, CancellationToken cancellationToken = default(CancellationToken)) where T : class;

Source

因此设置程序需要返回Task派生的结果以允许异步代码

var response = await Client.GetAsync<Dictionary<string, object>>(request);

按预期流动。

例如

[TestMethod]
[ExpectedException(typeof(NotFoundException))]
public async Task Get_ClientReturns404_ThrowsNotFoundException() {

    //Arrange
    var originalException = new Exception("Status code 404");

    var response = A.Fake<Nest.IGetResponse<Dictionary<string, object>>>();
    A.CallTo(() => response.OriginalException).Returns(originalException);

    var client = A.Fake<Nest.IElasticClient>();
    A.CallTo(() => 
        client.GetAsync<Dictionary<string, object>>(A<IGetRequest>._, A<CancellationToken>._)
    ).Returns(Task.FromResult(response));

    var request = new DataGetRequest {
        CollectionName = string.Empty,
        DocumentType = string.Empty,
        DataAccessType = string.Empty
    };

    var elasticSearch = new ElasticSearch(null, client);

    // Act
    var result = await elasticSearch.Get(request);

    // Assert
    Assert.Fail("Should have hit an exception.");
}