2012-03-27 44 views
0

以下this教程和遇到麻烦。Windows Phone测试失败

[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 
+0

通过外观的东西值NonconistantPropertyName被标记为无效和抛出的异常。你能澄清一下你的问题吗?或许可以详细说明你在哪里挣扎? – anothershrubery 2012-03-27 11:47:58

回答

4

您预期的例外是错误的类型。它应该是:

[ExpectedException(typeof(VerifyPropertyNameException))] 

这就是本教程显示为好,所以我不知道为什么你有typeof(Exception),而不是...

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