lundi 26 janvier 2015

Unit testing exception property

I have exception



class SyntaxError : Exception {
public SyntaxError(int l) {
line = l;
}
public int line;
}


I'm using unit tests to test class Parser which on specific input should throw exception above. I'm using code like this:



[TestMethod]
[ExpectedException(typeof(Parser.SyntaxError))]
public void eolSyntaxError()
{
parser.reader = new StringReader("; alfa\n; beta\n\n\n\na");
parser.eol();
}


Is there any smart simple way to check if SyntaxError.line == 1?


Best I come up with is:



[TestMethod]
public void eolSyntaxError()
{
try {
parser.reader = new StringReader("; alfa\n; beta\n\n\n\na");
parser.eol();
Assert.Fail();
} catch (SyntaxError e) {
Assert.AreEqual(1, e.line);
}
}


I don't like it very much, is there better way?


Aucun commentaire:

Enregistrer un commentaire