【发布时间】: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