gpt4 book ai didi

c# - FluentAssertions ShouldNotThrow 无法识别异步方法/Func

转载 作者:可可西里 更新时间:2023-11-01 08:21:23 25 4
gpt4 key购买 nike

我正在尝试检查异步方法抛出的具体异常。

为此,我使用 MSTEST 和 FluentAssertions 2.0.1。

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

尝试使用我的“生产”代码一段时间后,我关闭了 Fluentassertions 伪造的 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 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 版本已经支持 ShouldThrow。

最佳答案

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

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

asyncFunction.ShouldNotThrow();
}

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

关于c# - FluentAssertions ShouldNotThrow 无法识别异步方法/Func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com