异步方法/Func无法识别FluentAssertions ShouldNotThrow [英] FluentAssertions ShouldNotThrow is not recognised for an async method/Func

查看:107
本文介绍了异步方法/Func无法识别FluentAssertions ShouldNotThrow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查异步方法引发具体异常.

I am trying to check an async method throws concrete exception.

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

For that I am using MSTEST and FluentAssertions 2.0.1.

我已经检查了关于Codeplex的讨论,并查看了它如何与异步异常方法一起使用这是有关 FluentAssertions的链接/a>:

I have checked this Discussion on Codeplex and to see how it works with async-exception methods this another one link about FluentAssertions async tests:

尝试使用我的生产"代码一段时间后,我关闭了Fluentassertions伪aync类,得到的代码是这样的(将此代码放在[TestClass]中:

After a while trying to work with my 'production' code I have switched off to the Fluentassertions fake aync class and my resulting code is like this (put this code inside a [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, 字符串,params object [])'有一些无效的参数

ShouldNotThrow method is not recognised by the code. If I try to compile, it gives me this error: 'System.Func' does not contain a definition for 'ShouldNotThrow' and the best extension method overload 'FluentAssertions.AssertionExtensions.ShouldNotThrow(System.Action, string, params object[])' has some invalid arguments

谢谢.

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

2.0.1 FA version doesn't support this ShouldNotThrow functionality and it will be included in the next reléase 2.1 (near next week).

注意:2.0.1版本已支持ShouldThrow.

Note: ShouldThrow is already supported in 2.0.1 versión.

推荐答案

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

You don't need the encompassing Action. That is only used in the unit tests to verify that the API is throwing the right exception. This should be sufficient:

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

    asyncFunction.ShouldNotThrow();
}

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

Unfortunately the ShoudlNotThrow() on a Func is missing from .NET 4.5. I've fixed this in release 2.1 (currently dogfooding).

这篇关于异步方法/Func无法识别FluentAssertions ShouldNotThrow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆