遵循this教程,遇到了麻烦。
[TestMethod]
[ExpectedException(typeof(Exception))]
public void VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException()
{
var customer = new Customer() { FirstName = "June", LastName = "Smith" };
var sut = new CustomerViewModel(_customerRepository, customer);
sut.VerifyPropertyName("NonExistentPropertyName");
}测试失败,并显示以下消息。测试显然抛出了一个异常,但它应该抛出异常!为什么测试会失败呢?
VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException : FailedTest method FirstOnSiteWindowsPhoneApp.AppCode.Tests.Unit.CustomerViewModelTests.VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException threw exception:
FirstOnSiteWindowsPhoneApp.AppCode.Domain.VerifyPropertyNameException: Exception of type 'FirstOnSiteWindowsPhoneApp.AppCode.Domain.VerifyPropertyNameException' was thrown.
at FirstOnSiteWindowsPhoneApp.AppCode.ViewModel.CustomerViewModel.VerifyPropertyName(String propertyName) in CustomerViewModel.cs: line 29
at FirstOnSiteWindowsPhoneApp.AppCode.Tests.Unit.CustomerViewModelTests.VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException() in CustomerViewModelTests.cs: line 53发布于 2012-03-27 19:46:19
您预期的异常类型错误。它应该是:
[ExpectedException(typeof(VerifyPropertyNameException))]这也是本教程所展示的,所以我不确定为什么您要使用typeof(Exception)……
ExpectedException完全需要指定类型的异常,而不仅仅是从它派生的任何类型的异常。请注意,就我个人而言,我更喜欢Assert.Throws<...>(() => ...),因为这样可以限制预期抛出的代码的范围,但这是另一回事。
https://stackoverflow.com/questions/9888894
复制相似问题