2008 Advent Calendar December 14th

 

   1:      public class Advent14
   2:      {
   3:          private IFileUtil m_file;
   4:   
   5:          private void SetUpReadable(string content)
   6:          {
   7:              m_file = new FileUtilWithDelete(Path.GetTempFileName());
   8:              m_file.Create(content);
   9:          }
  10:   
  11:          private void SetUpUnreadable(string content)
  12:          {
  13:              SetUpReadable(content);
  14:              m_file.Readable = false;
  15:          }
  16:   
  17:          [Fact]
  18:          public void TestReadReadableFile()
  19:          {
  20:              SetUpReadable("CONTENT");
  21:              string content = m_file.Read();
  22:              Assert.Equal<string>("CONTENT", content);
  23:          }
  24:   
  25:          [Fact]
  26:          public void TestReadUnreadableFile()
  27:          {
  28:              SetUpUnreadable("SHOULD NOT BE ABLE TO READ THIS");
  29:              Assert.Throws<AccessViolationException>(() => { m_file.Read(); });
  30:          }
  31:      }

The use of the new FileUtilWithDelete-class makes the tests neater than before I think. But the member variable should also be removed since it is a legacy from the time when we used the unit test framework setup-method. And we need to create a unique file name in order to get rid of the Dispose method. The use of a temporary (unknown) name makes it hard to find the file if something goes wrong and it is left on the host. So I want to get rid of that as soon as possible too.