我试图检查异步方法抛出具体异常.
为此我使用的是MSTEST和FluentAssertions 2.0.1.
我已经检查了Codeplex上的这个讨论,并看看它如何与异步异常方法一起工作,这是另一个关于FluentAssertions异步测试的链接:
尝试使用我的'生产'代码一段时间之后,我已经关闭了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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是ShouldNotThrow无效:
代码无法识别ShouldNotThrow方法.如果我尝试编译,它会给我这个错误:'System.Func'不包含'ShouldNotThrow'的定义和最好的扩展方法重载'FluentAssertions.AssertionExtensions.ShouldNotThrow(System.Action,string,params object []) …