2008 Advent Calendar December 2nd




1: public class Advent2
2: {
3: [Fact]
4: public void TestRead()
5: {
6: // Create a test file
7: IFileUtil file = new FileUtil(“SomeFile.txt”);
8: file.Create(“CONTENT”);
9:  
10: // Should be able to read file
11: try
12: {
13: file.Read();
14: }
15: catch (Exception)
16: {
17: throw new AssertException(“Unexpected exception thrown”);
18: }
19:  
20: file.Readable = false;
21:  
22: // Should NOT be able to read file.
23: try
24: {
25: file.Read();
26: throw new AssertException(“No exception thrown”);
27: }
28: catch (AccessViolationException)
29: {
30: }
31: catch (Exception)
32: {
33: throw new AssertException(“Unexpected exception thrown”);
34: }
35:  
36: // Clean up
37: file.Delete();
38: }
39: }

Now I’ve added a handler for unexpected exceptions but this is still a pretty cumbersome way to write tests verifying that exceptions are thrown or not correctly. Especially since Xunit.net provides us with a really simple way to handle this for us. That is what will happen next.

Comments (0)