Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IFileSystemInteractor
- {
- StreamReader OpenText(string inputFilePath);
- StreamWriter AppendText(string outputFilePath);
- }
- public class FileSystemInteractor : IFileSystemInteractor
- {
- public StreamReader OpenText(string inputFilePath)
- {
- return File.OpenText(inputFilePath);
- }
- public StreamWriter AppendText(string outputFilePath)
- {
- return File.AppendText(outputFilePath);
- }
- }
- public class FileSystemInteractorMock : IFileSystemInteractor
- {
- private readonly MemoryStream _inputMemoryStream;
- public MemoryStream OutputMemoryStream { get; }
- public FileSystemInteractorMock(string fileContent)
- {
- _inputMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent));
- OutputMemoryStream = new MemoryStream();
- }
- public StreamReader OpenText(string inputFilePath)
- {
- var streamReader = new StreamReader(_inputMemoryStream);
- return streamReader;
- }
- public StreamWriter AppendText(string outputFilePath)
- {
- var streamWriter = new StreamWriter(OutputMemoryStream);
- return streamWriter;
- }
- }
- [TestFixture]
- public class Tests
- {
- [Test]
- public void Examined_Data_Written_Correctly_To_File()
- {
- var inputFilePath = "inputFile.txt";
- var outputFilePath = "outputFile.txt";
- var inputFileContents = string.Join(Environment.NewLine, new[] {"OK", "WRONG", "OK", "WRONG"});
- var fileInteractorMock = new FileSystemInteractorMock(inputFileContents);
- var sut = new ProductionClass(fileInteractorMock);
- var resultAsync = sut.CheckDataAndStoreResultsAsync(inputFilePath, outputFilePath);
- resultAsync.Wait();
- var outputLines = Encoding.UTF8.GetString(fileInteractorMock.OutputMemoryStream.ToArray())
- .Split(new [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
- CollectionAssert.AreEquivalent(new []{"1", "3"}, outputLines, $"Actual:{string.Join(",", outputLines)}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement