I'm using Dotcover to see the coverage that my unittests have of my production code. It works quite well except when I'm trying to test thrown exceptions and that is my question what I'm doing wrongly there. I've tried both variants below already and only get a coverage of 92% or 89%.
As a note on the unit testing framework: I'm using no external/additional framework there but the Unittest framework that comes with Visual studio itself.
[TestMethod]
[ExpectedException(typeof(FormatException), "No exception or exception of wrong type thrown")]
public void TestException()
{
new myClass().DatabaseID = 0;
}
[TestMethod]
public void TestException()
{
try {
new myClass().DatabaseID = 0;
Assert.Fail
}
catch (Exception ex)
{
Assert.IsTrue (ex is FormatException);
}
}
I've also tried with a formatexception in the catch and another catch around it, but still the highest I get is 92% and else way less down to 16% even. So Like mentioned my question here is what am I doing wrong there?
As additional info about myclass it has the following Property and private variable:
private int _DatabaseID;
public int DatabaseID
{
get { return _DataBaseID; }
set { if (value != 0) {_DatabaseID = value; } else { throw new FormatException ("haha"); }
}