Reputation: 311
Following this tutorial and running into trouble.
[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");
}
The test fails with the message seen below. The test obviosuly throws an exception, but it is supposed to! Why does the test then fail?
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
Upvotes: 0
Views: 80
Reputation: 1504162
Your expected exception is of the wrong type. It should be:
[ExpectedException(typeof(VerifyPropertyNameException))]
That's what the tutorial shows as well, so I'm not sure why you've got typeof(Exception)
instead...
ExpectedException
expects exactly the specified type of exception, not just anything deriving from it. Note that personally I prefer Assert.Throws<...>(() => ...)
as that way you limit the scope of the code which is expected to throw, but that's a separate matter.
Upvotes: 4