fer*_*ega 12 c# async-await fluent-assertions
我试图检查异步方法抛出具体异常.
为此我使用的是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 []) '有一些无效的论点
谢谢.
2.0.1 FA版本不支持此ShouldNotThrow功能,它将包含在下一个版本2.1(下周附近)中.
注意:2.0.1版本中已经支持ShouldThrow.
Den*_*men 16
你不需要包含行动.这仅用于单元测试,以验证API是否正在抛出正确的异常.这应该足够了:
[TestMethod]
public void TestThrowFromAsyncMethod()
{
Func<Task> asyncFunction = async () =>
{
await asyncObject.ThrowAsync<ArgumentException>();
};
asyncFunction.ShouldNotThrow();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,.NET 4.5中缺少Func上的ShoudlNotThrow().我已经在2.1版(目前是dogfooding)中解决了这个问题.
| 归档时间: |
|
| 查看次数: |
11149 次 |
| 最近记录: |