public void TestProperlyDisposedReader() { var file = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, @"Disposing\Resources\TextFile1.txt")); var goodConsumer = new GoodStreamConsumer(); //so, it works first time it's used Assert.IsNotNull(goodConsumer.GetFirstLine(file)); //and again... Assert.IsNotNull(goodConsumer.GetFirstLine(file)); //and even if you create a new instance of the consumer (duh!) Assert.IsNotNull(new GoodStreamConsumer().GetFirstLine(file)); }
public void TestFileAccessedInDifferentScope() { //this file was accessed in the constructor of a different class, called by a constructor - that's not visible, but still the file is not available FileInfo file = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, @"Disposing\Resources\TextFile3.txt")); GoodStreamConsumer goodConsumer = new GoodStreamConsumer(); //even a proper reader won't handle it Assert.That(() => goodConsumer.GetFirstLine(file), Throws.Exception.TypeOf(typeof(IOException)) .With.Message.Contain("The process cannot access the file") .And.With.Message.Contain(" because it is being used by another process")); }