gpt4 book ai didi

c# - 如何在 Nest 中测试异常?

转载 作者:行者123 更新时间:2023-11-30 22:58:05 25 4
gpt4 key购买 nike

当 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);

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

但是,当单步执行代码时,当客户端被调用时,它返回一个伪造的响应,但是 OriginalException getter 没有值。它不为空,但没有任何属性具有任何值。我期望 OriginalException.Message 等于 Status code 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 block 中的错误处理不是很可靠。一旦单元测试成功,代码可能会受到更多的喜爱。

最佳答案

模拟客户端的返回类型是错误的 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.");
}

关于c# - 如何在 Nest 中测试异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53232285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com