What is the difference between the ExpectedException() attribute and Assert.Throws()?
With Assert.Throws() you can be really specific. You can say: an exception will be thrown at this method.
Whereas if you use ExpectedException: you do not have that level of granularity: the exception will be thrown somewhere during the test.
Furthermore, with Assert.Throws, you can test the exception that is returned itself.
[Test]
[Category("Slow")]
public void IsValidLogFileName_nullFileName_ThrowsExcpetion()
{
// the exception we expect thrown from the IsValidFileName method
var ex = Assert.Throws<ArgumentNullException>(() => a.IsValidLogFileName(""));
// now we can test the exception itself
Assert.That(ex.Message == "Blah");
}
Have you been paying attention?
If so please answer: why would you use Assert.Throws over ExpectedException?
Written on December 6, 2016