pszczyg

MockingFileSystem_1

May 28th, 2021 (edited)
716
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public class ProductionClass
  2.     {
  3.         public async Task CheckDataAndStoreResultsAsync(string inputFilePath, string outputFilePath)
  4.         {
  5.             using (var inputFileStream = File.OpenText(inputFilePath))
  6.             {
  7.                 using (var outputFileStream = File.AppendText(outputFilePath))
  8.                 {
  9.                     string inputLine;
  10.                     int lineCounter = 1;
  11.                     while ((inputLine = await inputFileStream.ReadLineAsync()) != null)
  12.                     {
  13.                         if (inputLine.Contains("OK"))
  14.                         {
  15.                             await outputFileStream.WriteLineAsync(lineCounter.ToString());
  16.                         }
  17.  
  18.                         lineCounter++;
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.     }
  24.  
  25.     [TestFixture]
  26.     public class Tests
  27.     {
  28.         [Test]
  29.         public void Examined_Data_Written_Correctly_To_File()
  30.         {
  31.             var inputFilePath = "inputFile.txt";
  32.             var outputFilePath = "outputFile.txt";
  33.             var inputFileContents = new[] {"OK", "WRONG", "OK", "WRONG"};
  34.             File.WriteAllLines(inputFilePath, inputFileContents);
  35.             File.Delete(outputFilePath);
  36.            
  37.             var sut = new ProductionClass(new FileSystemInteractor());
  38.             var resultAsync = sut.CheckDataAndStoreResultsAsync(inputFilePath, outputFilePath);
  39.             resultAsync.Wait();
  40.  
  41.             var outputLines = File.ReadAllLines(outputFilePath);
  42.             CollectionAssert.AreEquivalent(new []{"1", "3"}, outputLines, $"Actual:{string.Join(",", outputLines)}");
  43.         }
  44.     }
RAW Paste Data