2008 Advent Calendar December 4th




1: public class Advent4
2: {
3: [Fact]
4: public void TestReadOK()
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: Assert.DoesNotThrow(() => { file.Read(); });
12:  
13: // Clean up
14: file.Delete();
15: }
16:  
17: [Fact]
18: public void TestReadFails()
19: {
20: // Create a test file
21: IFileUtil file = new FileUtil(“SomeFile.txt”);
22: file.Create(“CONTENT”);
23:  
24: file.Readable = false;
25:  
26: // Should NOT be able to read file.
27: Assert.Throws<AccessViolationException>(() => { file.Read(); });
28:  
29: // Clean up
30: file.Delete();
31: }
32: }

Now that the initial test has been split up into two tests. One testing reading a readable file and one testing an unreadable file. Now I got some redundant code I wanna get rid of. The first thing is the clean up code. It is not run if the test fails and that is probably a bad thing. So next I’ll fix that.

Comments (0)