我已经通过实现NUnit.Framework.IExpectException
接口创建了一个预期的异常属性,如那里所述(http://www.nunit.org/index.php?p=exception&r=2.6.2),它工作得很好。它验证抛出的异常是否属于正确的类型,并验证异常上某些属性的值。
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ExpectedHttpErrorExceptionAttribute : ExpectedExceptionAttribute, IExpectException
{
public HttpStatusCode StatusCode { get; set; }
public ExpectedHttpErrorExceptionAttribute()
:base(typeof(HttpError))
{}
public ExpectedHttpErrorExceptionAttribute(HttpStatusCode statusCode)
:this()
{
StatusCode = statusCode;
}
public void HandleException(Exception ex)
{
Assert.That(ex, Is.TypeOf(ExpectedException), "Expected exception of type '{0}' but an exception of type '{1}' has been throwned.", typeof(HttpError).FullName, ex.GetType().FullName);
var httpStatusCode = ((HttpError) ex).StatusCode;
Assert.That(httpStatusCode, Is.EqualTo(StatusCode), "Expected status code '{0}' but was '{1}'.", StatusCode, httpStatusCode);
}
}
我的问题是我想对该属性进行单元测试,但我想不出一种方法来测试以下情况:
A unit test that have the attribute should fail if no exception are thrown.
我只是想不出一种方法来为这种情况编写单元测试,因为当没有抛出异常时不会调用 HandleException。
有什么建议吗?