当前位置 : 主页 > 编程语言 > c语言 >

c# – 应该抛出单元测试失败,但是传奇处理程序代码确实会抛出一个不可恢复的

来源:互联网 收集:自由互联 发布时间:2021-06-25
在我的测试构造函数中,我设置了我的传奇: public When_Testing_My_Saga(){ _mySaga = new MySaga { Data = new MySaga.MySagaData() };} 我的测试断言未接收重要数据会导致失败: [Fact]public void Not_Providing_Da
在我的测试构造函数中,我设置了我的传奇:

public When_Testing_My_Saga()
{
    _mySaga = new MySaga
    {
        Data = new MySaga.MySagaData()
    };
}

我的测试断言未接收重要数据会导致失败:

[Fact]
public void Not_Providing_Data_Should_Cause_A_Failure()
{
    var context = new TestableMessageHandlerContext();

    Should.Throw<NoDataProvidedFailure>(() =>
    {
        _mySaga.Handle(new ImportDataReadMessage
        {
            ImportantData = null
        }, context).ConfigureAwait(false);
    });
}

SqlSaga中的实际代码:

public async Task Handle(ImportantDataReadMessage message, IMessageHandlerContext context)
{
    if (message.ImportantData == null)
    {
        throw new NoDataProvidedFailure("Important data was not provided.");
    }

    await context.Send(Endpoints.MyEndpoint, new DoStuffWhenImportantDataProvided
    {
        Reference = message.Reference
    });
}

引发预期的失败,但测试表明相反:

Shouldly.ShouldAssertException _mySaga.Handle(new
ImportantDataReadMessage { Reference = string.Empty, ImportantData =
null }, context).ConfigureAwait(false);

should throw Service.Failures.NoDataProvidedFailure
but did not at Not_Providing_Data_Should_Cause_A_Failure () in mypath\When_Testing_My_Saga.cs:line 77

这真的很奇怪,因为如果我调试到处理程序,抛出线就会被击中.

有什么线索可能会发生什么?

PS:NoDataProvidedFailure继承自Exception,但被称为失败,表明它不可恢复(不会触发重试).

应该能够使用Do.ThrowAsync与Func< Task>在正确的线程上捕获异常以允许按预期执行测试.

[Fact]
public async Task Not_Providing_Data_Should_Cause_A_Failure() {
    //Arrange
    var context = new TestableMessageHandlerContext();

    //Act
    Func<Task> act = () =>  _mySaga.Handle(new ImportDataReadMessage
                                {
                                    ImportantData = null
                                }, context);

    //Assert
    await Should.ThrowAsync<NoDataProvidedFailure>(act);
}
网友评论