Windows Phone测试失败

时间:2012-03-27 11:42:24

标签: c# unit-testing windows-phone-7

关注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

1 个答案:

答案 0 :(得分:4)

您预期的异常类型错误。它应该是:

[ExpectedException(typeof(VerifyPropertyNameException))]

这也就是教程所展示的内容,所以我不确定为什么你会typeof(Exception)代替......

ExpectedException期望完全指定的异常类型,而不仅仅是从中派生的任何异常。请注意,我个人更喜欢Assert.Throws<...>(() => ...),因为这样可以限制预期抛出的代码范围,但这是一个单独的问题。