繁体   English   中英

FluentAssertions Throw() 未列出使用

[英]FluentAssertions Throw() not listed to use

我在 NUnit 中使用 FluentAssertions,我意识到 Throw() 方法和其他相关方法没有列出供我使用。 我是否必须安装任何其他软件包才能访问此方法?

我使用的是 NuGet 安装的最新版本 5.4.2。

文档没有非常清楚,但Should().Throw()被应用到行动(或者,如下面,一个评论由@ArturKrajewski指出Funcasync调用):

Action test = () => throw new InvalidOperationException();
test.Should().Throw<InvalidOperationException>();

所以测试可能是这样的:

public class AssertThrows_ExampleTests {
    [Test]
    public void Should_Throw_Action() {
        var classToTest = new TestClass();

        // Action for sync call
        Action action = () => classToTest.MethodToTest();
        action.Should().Throw<InvalidOperationException>();
    }

    [Test]
    public void Should_Throw_Action_Async() {
        var classToTest = new TestClass();

        // Func<Task> required here for async call
        Func<Task> func = async () => await classToTest.MethodToTestAsync();
        func.Should().Throw<InvalidOperationException>();
    }

    private class TestClass {
        public void MethodToTest() {
            throw new InvalidOperationException();
        }

        public async Task MethodToTestAsync() {
            throw new InvalidOperationException();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM