2008 Advent Calendar December 13th

 

   1:      public class Advent13 : IDisposable
   2:      {
   3:          private IFileUtil m_file;
   4:   
   5:          private void SetUpReadable(string content)
   6:          {
   7:              m_file = new FileUtil("SomeFile.txt");
   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:          public void Dispose()
  18:          {
  19:              m_file.Delete();
  20:          }
  21:   
  22:          [Fact]
  23:          public void TestReadReadableFile()
  24:          {
  25:              SetUpReadable("CONTENT");
  26:              string content = m_file.Read();
  27:              Assert.Equal<string>("CONTENT", content);
  28:          }
  29:   
  30:          [Fact]
  31:          public void TestReadUnreadableFile()
  32:          {
  33:              SetUpUnreadable("SHOULD NOT BE ABLE TO READ THIS");
  34:              Assert.Throws<AccessViolationException>(() => { m_file.Read(); });
  35:          }
  36:      }

So far we've used the unit test framework clean up mechanism to remove the test file after each test. I don't see anything wrong with that but another way to do it would be to introduce a new class that does this for us. Tomorrow I'll make use of this new class:

     public class FileUtilWithDelete : FileUtil, IDisposable
    {
        public FileUtilWithDelete(string path)
            : base(path)
        {
            
        }

        public void Dispose()
        {
            Delete();
        }
    }