内例外,我有一个单元测试,是这样的:我的单元测试是不是抓我控制器操作
[Test]
public void ThingController__Put__when_thing_is_invalid__then__throws()
{
var controller = this.CreateThingController();
try
{
var r = controller.Put("thing1", this.CreateInvalidThing());
}
catch(HttpResponseException hrex) when (hrex.Response.StatusCode == HttpStatusCode.BadRequest)
{
return; // implicit pass.
}
catch(Exception ex)
{
Assert.Fail($"Wrong exception {ex.GetType().Name}");
}
Assert.Fail("No exception thrown!");
}
但它总是命中最后Fail
,即不会抛出异常。我已经打开了第一次机会异常,并可以看到它被抛出,并一直重新出现。它一直在冒泡。
注:SO检举这个的
Unit testing async method for specific exception
这个问题可能重复的是如何做,但是这个人是一个问题的解决方案,专门针对为什么catch
没有被击中,即你知道如何,但犯了一个常见的错误,忘记行动是异步的 - 因为他们通常没有Async
后缀 - 并需要展开。
(http://stackoverflow.com/questions/12837128/unit-testing-async-method-for-specific-exception)为特定异常单元测试异步方法]的可能的复制 –