【问题标题】:FluentAssertions ShouldNotThrow is not recognised for an async method/Func异步方法/Func 无法识别 FluentAssertions ShouldNotThrow
【发布时间】:2013-08-16 21:50:10
【问题描述】:

我正在尝试检查异步方法是否引发了具体异常。

为此,我正在使用 MSTEST 和 FluentAssertions 2.0.1。

我已经检查了这个 Discussion on Codeplex 并查看它如何与异步异常方法一起工作,这是另一个关于 FluentAssertions async tests 的链接:

尝试使用我的“生产”代码一段时间后,我关闭了 Fluentassertions fake aync 类,结果代码如下所示(将此代码放入 [TestClass]:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    var asyncObject = new AsyncClass();
    Action action = () =>
    {
        Func<Task> asyncFunction = async () =>
        {
            await asyncObject.ThrowAsync<ArgumentException>();
        };
        asyncFunction.ShouldNotThrow();
    };
}


internal class AsyncClass
{
    public async Task ThrowAsync<TException>()
        where TException : Exception, new()
    {
        await Task.Factory.StartNew(() =>
        {
            throw new TException();
        });
    }

    public async Task SucceedAsync()
    {
        await Task.FromResult(0);
    }
}

问题是ShouldNotThrow无效:

代码无法识别ShouldNotThrow 方法。如果我尝试 编译,它给了我这个错误: 'System.Func' 不包含 'ShouldNotThrow' 的定义和最佳扩展方法重载 'FluentAssertions.AssertionExtensions.ShouldNotThrow(System.Action, string, params object[])' 有一些无效参数

谢谢。


解决方案

2.0.1 FA 版本不支持此ShouldNotThrow 功能,它将包含在下一个版本 2.1(下周附近)中。

注意:ShouldThrow 已在 2.0.1 版本中得到支持。

【问题讨论】:

  • 我对 FluentAssertions 一无所知,但例外情况是说 ShouldNotThrow 仅针对 Action 而不是 Func 定义。
  • @KeithPayne 这意味着 FluentAsseritions 可能不支持async
  • FluentAssertions 支持异步,如果你阅读上面的链接(链接),你可以看到它。这个也可以提供帮助:fluentassertions.codeplex.com/workitem/12148
  • @ferpega:您的单元测试项目中的目标是 .NET 4.5 吗?如果是这样,请尝试卸载 FA 并重新安装。
  • @StephenCleary: Target is .NET 4.5 FluentAssertions is Nuget managed and runtime-version-property is: v4.0.30319

标签: c# async-await fluent-assertions


【解决方案1】:

您不需要包含 Action。这仅在单元测试中用于验证 API 是否抛出了正确的异常。这应该足够了:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    Func<Task> asyncFunction = async () =>
    {
        await asyncObject.ThrowAsync<ArgumentException>();
    };

    asyncFunction.ShouldNotThrow();
}

不幸的是,.NET 4.5 中缺少 Func 上的 ShoudlNotThrow()。我已经在 2.1 版中修复了这个问题(目前是 dogfooding)。

【讨论】:

  • 发布代码时,请将其突出显示并点击{} 按钮(如果需要练习,也可以手动将每行缩进四个空格)。
  • @DennisDomen 我知道,我只是在编写通过 FA 测试的完全相同的代码。但是,如果我的框架已经是 4.5,为什么 FA 安装 4.0.... 与 nuget ?
  • @DennisDoomen 我的意思是,FA 是运行时版本4.0.30319,它已经位于net45 文件夹中,完整路径为:....\packages\FluentAssertions.2.0.1\lib\net45\FluentAssertions.dll。但是没有ShouldNotThrow 可用。
  • 嗯。我需要尝试一下。 4.0.30319 = .NET 4.5
  • 如果这个方法被暴露为“异步”,这样就可以等待它了。 VS 和 Resharper 都抱怨没有正确实现等待/异步。请参阅 MS 的 Assert.ThrowsExceptionAsync()
【解决方案2】:

如果您查看AssertionExtensions.cs class,您会发现 Func 上的 ShouldNotThrow 扩展方法仅针对 net45 或 winrt 编译目标定义。

检查一下:

  1. 您的单元测试项目位于 .net 4.5 或 winrt 上
  2. 引用的断言库是 .net 4.5 的,如果没有尝试更改 在右侧引用了 FluentAssertions 库。

同样做了这个之后,我觉得你需要调用action方法来做断言,否则内部的lambda不会被调用:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    var asyncObject = new AsyncClass();
    Action action = () =>
    {
        Func<Task> asyncFunction = async () =>
        {
            await asyncObject.ThrowAsync<ArgumentException>();
        };
        asyncFunction.ShouldNotThrow();
    };

    action.ShouldNotThrow();
}

【讨论】:

  • 目标是 4.5,但运行时版本是:v4.0.30319。 FluentAssertions 由 Nuget 管理。
  • 请检查引用的 FluentAssertions 库是否来自 nuget 包文件夹中名为“net45”的正确文件夹。
  • 在即将发布的 FA 2.1 中,这些扩展方法将支持 .NET 4.0
  • @DennisDomen 我很不耐烦。 :-)
  • @iCe:是的,FluentAssertions 库位于 net45 文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2023-03-25
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2023-03-16
相关资源
最近更新 更多