2008 Advent Calendar December 5th
1: public class Advent5 : IDisposable
2: {
3: private IFileUtil m_file;
4:
5: public void Dispose()
6: {
7: m_file.Delete();
8: }
9:
10: [Fact]
11: public void TestReadOK()
12: {
13: // Create a test file
14: m_file = new FileUtil("SomeFile.txt");
15: m_file.Create("CONTENT");
16:
17: // Should be able to read file
18: Assert.DoesNotThrow(() => { m_file.Read(); });
19: }
20:
21: [Fact]
22: public void TestReadFails()
23: {
24: // Create a test file
25: m_file = new FileUtil("SomeFile.txt");
26: m_file.Create("CONTENT");
27:
28: m_file.Readable = false;
29:
30: // Should NOT be able to read file.
31: Assert.Throws<AccessViolationException>(() => { m_file.Read(); });
32: }
33: }
Now we're using Xunit-net's clean up mechanism, the IDisposableinterface. We still got some redundant code in the setup part of each test. Next I'll refactor that.