C # testing - why is ExpectedException method flagged when no exception is thrown?
I've tested expected exceptions in the past, for example:
[TestMethod]
public void TestThrowsException() {
try {
Foo();
Assert.Fail();
} catch (MyException ex){//good
}
}
However, I notice that there is a (clean up?) Way to test this using the ExpectedException attribute. Why does this testing method fail when no exception is thrown? Surely this defeats the purpose of the attribute.
[TestMethod]
[ExpectedException(typeof(MyException))]
public void TestThrowsException() {
}
[Edit] I am running this test with Silverlight 2
a source to share
Jon Skeet was actually right, I had an old version of the testing framework. I updated to Dec 08 release here http://code.msdn.microsoft.com/silverlightut/Release/ProjectReleases.aspx?ReleaseId=1913 and got expected behavior when flagged with ExpectedException.
a source to share
You have to use "ExpectedException" with the optional argument assert.fail to make the test fail if no exception is thrown (still in VS 2010 with .net 4 and Microsoft.VisualStudio.QualityTools.UnitTestFramework):
[TestMethod]
[ExpectedException(typeof(MyException))]
public void TestThrowsException() {
Do.SomethingThatThrowsAnException();
assert.fail("No exception ;-(");
}
a source to share