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

How do I mock PolicyResult to contain a value for FinalException?

我创建了一个摘要 class,它实现了我想为其编写单元测试的 Polly。 在我的一项测试中,我想测试我的方法如何处理 PolicyResult.FinalException 的某些值。 因为返回的 PolicyResult 是 null 我在评估 result.FinalException 时得到 NullReferenceException 如何模拟返回结果?

我目前拥有的:

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";
}

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

根据 Nkosi 的评论,我了解得更深入了一些,但 _policy.ExecuteAndCaptureAsync 返回的 PolicyResult 仍然是 null。这使我相信我嘲笑该方法的方式有问题。 我将测试更改为以下内容,但仍然 returns `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));
}

我评估了 ExecuteAndCapture 所需的参数并相应地更改了此方法的设置,我做错了什么?

根据 GitHub 上公开可用的 source code,确实没有必要嘲笑 class。虽然它确实有一个内部构造函数,但存在应该允许创建所需实例的静态工厂方法

例如

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()));
}