public void TestNotDisposedReader() { var file = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, @"Disposing\Resources\TextFile2.txt")); var badConsumer = new BadStreamConsumer(); //so, it works first time it's used Assert.IsNotNull(badConsumer.GetFirstLine(file)); //but then on a second access to that file it fails Assert.That(() => badConsumer.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")); //even if you create a new instance of the consumer (duh!) Assert.That(() => new BadStreamConsumer().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")); //and even if you use the proper consumer (duh x2!) Assert.That(() => new GoodStreamConsumer().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")); }
public SomeWrapperClass() { //this class simply locks a file in a non-obvious and incorrect way var file = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, @"Disposing\Resources\TextFile3.txt")); var badConsumer = new BadStreamConsumer(); Assert.IsNotNull(badConsumer.GetFirstLine(file)); }