2008 Advent Calendar December 10th
1: public class Advent10 : IDisposable
2: {
3: private IFileUtil m_file;
4:
5: private void SetUp(string content)
6: {
7: m_file = new FileUtil("SomeFile.txt");
8: m_file.Create(content);
9: }
10:
11: public void Dispose()
12: {
13: m_file.Delete();
14: }
15:
16: [Fact]
17: public void TestReadOK()
18: {
19: SetUp("CONTENT");
20: string content = "";
21: Assert.DoesNotThrow(() => { content = m_file.Read(); });
22: Assert.Equal<string>("CONTENT", content);
23: }
24:
25: [Fact]
26: public void TestReadFails()
27: {
28: SetUp("SHOULD NOT BE ABLE TO READ THIS");
29: m_file.Readable = false;
30: Assert.Throws<AccessViolationException>(() => { m_file.Read(); });
31: }
32: }
Some people think a good rule of thumb is to have only one assert in each test. I think it is OK to have more if the assert adds value if an unexpected failure happens. In this case I think line 21 does not really add value. If an exception is thrown the framework will report the unexpected exception anyway so let's get rid of that one.