如何模拟PolicyResult以包含FinalException的值?

问题描述

我创建了一个抽象类,该类实现了我想为其编写单元测试的Polly。 在我的测试之一中,我想测试我的方法如何处理{ $and: [ // grouping two conditions that Now time needs to be between the start and the end of the shift. { $or: [ // compare the start time hour of the shift first { 'Monday.startTime.hour': { $lt: parseInt(Now.format('HH'),10),},{ $and: [ // if the upper condition didn't work will need to compare hours and minutes { 'Monday.startTime.hour': { $lte: parseInt(Now.format('HH'),{ 'Monday.startTime.minute': { $lte: parseInt(Now.format('mm'),],{ $or: [ // compare the end time hour of the shift first { 'Monday.endTime.hour': { $gt: parseInt(Now.format('HH'),{ $and: [ // if the upper condition didn't work will need to compare hours and minutes { 'Monday.endTime.hour': { $gte: parseInt(Now.format('HH'),{ 'Monday.endTime.minute': { $gte: parseInt(Now.format('mm'),]; } 的某些值。 由于返回的PolicyResult.FinalException为空,因此我在评估PolicyResult时得到一个NullReferenceException 如何模拟返回的结果?

到目前为止我所拥有的:

result.FinalException

------------------编辑1 --------------------------- -----

使用Nkosi的评论,我可以进一步了解,但是public class AbstractRestClientTest { private AbstractRestClient _sut; private Mock<IRestRequestFactory> _requestFactoryMock; private Mock<IRestClientFactory> _restClientfactoryMock; private Mock<IPollyPolicyFactory> _policyFactoryMock; private Mock<IAsyncPolicy> _policyMock; private const string DUMMY_URL = "http://dosomething.com/getmesomething"; [SetUp] public void SetUp() { _requestFactoryMock = new Mock<IRestRequestFactory>(); _restClientfactoryMock = new Mock<IRestClientFactory>(); _policyFactoryMock = new Mock<IPollyPolicyFactory>(); var settings = new MockSettings(); _policyMock = new Mock<IAsyncPolicy>(); _policyFactoryMock.Setup(mock => mock.CreateAsyncResiliencePolicy(settings)) .Returns(_policyMock.Object); _sut = new MockRestClient(settings,_restClientfactoryMock.Object,_policyFactoryMock.Object,_requestFactoryMock.Object); } } public class MockRestClient : AbstractRestClient { public MockRestClient(RestSettings settings,IRestClientFactory restClientFactory,IPollyPolicyFactory pollyPolicyFactory,IRestRequestFactory requestFactory) : base(settings,restClientFactory,pollyPolicyFactory,requestFactory) { } } public class MockSettings : RestSettings { public override string Naam => "TestSettings"; } 返回的PolicyResult仍然是_policy.ExecuteAndCaptureAsync。这使我相信,我模拟该方法的方式存在问题。 我将测试更改为以下内容,但仍返回“ null”:

null

我评估了[Test] public async Task HandleRequest_IfFinalExceptionNotNull_ThenThrowsException() { var mockResult = new Mock<IRestResponse<int>>(); PolicyResult<IRestResponse<int>> result = PolicyResult<IRestResponse<int>>.Failure(mockResult.Object,new Context()); //Is the following mock correctly setup? _policyMock.Setup(mock => mock.ExecuteAndCaptureAsync(It.IsAny<Func<Task<IRestResponse<int>>>>())) .ReturnsAsync(result); var url = new Url(DUMMY_URL); Assert.ThrowsAsync<Exception>(() => _sut.GetResult<int>(url)); } 所需的参数,并相应地更改了此方法的设置,我在做什么错了?

解决方法

基于GitHub上公开可用的source code,确实不需要模拟该类。尽管它确实具有内部构造函数,但存在静态工厂方法,该方法应允许创建所需的实例

例如

Context context = //...created as needed

PolicyResult<TestResponse> result = PolicyResult<TestResponse>.Failure(...,context);

选择正确的组合来满足测试中的预期结果。

,

问题是我在模拟错误的ExecuteAndCaptureAsync版本,我需要使用以下签名模拟方法:

`Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken,Task> action,CancellationToken cancellationToken);`

因此,我相应地更改了SetUp后,测试成功了:

[Test]
public async Task HandleRequest_IfFinalExceptionNotNull_ThenThrowsException()
{
    var mockResult = new Mock<IRestResponse<int>>();
    PolicyResult<IRestResponse<int>> result = PolicyResult<IRestResponse<int>>.Failure(mockResult.Object,new Context());

    _policyMock.Setup(mock => mock.ExecuteAndCaptureAsync(
        It.IsAny<Func<CancellationToken,Task<IRestResponse<int>>>>(),It.IsAny<CancellationToken>()))
    .ReturnsAsync(result);

    var url = new Url(DUMMY_URL);
    Assert.ThrowsAsync<Exception>(() => _sut.GetResultaat(url,new CancellationToken()));
}