2012-03-27 65 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<...>(() => ...),因爲這樣可以限制預期會拋出的代碼範圍,但這是一個單獨的問題。