【发布时间】:2014-03-22 17:33:31
【问题描述】:
我想编写一个单元测试来确保我得到一个带有从特定方法抛出的 404 状态码的 WebException。
WebException 位很简单:
[Test]
[ExpectedException(typeof(WebFaultException))]
public void CheckForWebFaultException()
{
var myClass = new MyClass();
myClass.MyMethod();
}
但是,这可能是 404、400、401 或无数其他 http 代码中的任何其他代码。
我可以做一个 try/catch/Assert.True 但这感觉就像一个 hack。有没有办法针对抛出的异常的属性进行断言?
类似
Assert.Throws(typeof(WebFaultException), myClass.MyMethod(), wfx => wfx.StatusCode == HttpStatusCode.NotFound);
【问题讨论】:
标签: unit-testing exception-handling nunit