如何在Monodevelop上使用Moq框架在C#中读取文件时输出文本输出

问题描述:

我在这个周末一直在抨击我的头。基本上我正在做一个生命游戏的代码卡塔,它涉及阅读一个文本文件。我采用包含网格的二维表示的文本文件,并将所有点存储在List列表中。我试图嘲笑从文件获得的文本输入只是'\ n'一个新行,所以我可以编写单元测试,检查List列表中是否创建了一个新列表。我创建了一个文件包装来处理文本文件的阅读,这就是我想要嘲笑的。该代码符合要求,但测试失败并显示错误消息“System.ArgumentException:指定的路径不是合法表单”。它似乎仍然期待着一个文件路径,但嘲笑应该改变这种行为吗?任何帮助,将不胜感激。如何在Monodevelop上使用Moq框架在C#中读取文件时输出文本输出


using System.Collections.Generic; 

namespace GameOfLife 
{ 
    public class InitializeGrid 
    { 

     public InitializeGrid() 
     { 
     } 

     public List<List<char>> CreateCharMatrix (string filePathName) 
     { 
      // Reads in the text file containing the grid data 
      FileWrapper fileWrapper = new FileWrapper(); 
      string inputGridTextFile = fileWrapper.ReadTextFromFile (filePathName); 

      // Creates character matrix and initialises the first sub List 
      List<List<char>> charMatrix = new List<List<char>>(); 
      charMatrix.Add(new List<char>()); 

      int rowIndex = 0; 
      int colIndex = 0; 

      foreach (char cell in inputGridTextFile) { 
       if (cell == '\n') { 
        charMatrix.Add (new List<char>()); 
        rowIndex++; 
        colIndex = 0; 
       } else { 
        charMatrix [rowIndex] [colIndex] = cell; 
        colIndex++; 
       } 
      } 

      return charMatrix; 
     } 

    } 
} 

using NUnit.Framework; 
using System; 
using System.Collections.Generic; 
using Moq; 

namespace GameOfLife 

    [TestFixture()] 
    public class InitializeGridTest 
    { 
     [Test()] 
     public void CreateCharMatrix_EnsuresThatWhenEndOfLineReachedNewSubListCreated() 
     { 
      //Arrange 

      InitializeGrid initializeGrid = new InitializeGrid(); 
      List<List<char>> charMatrix; 
      string filePathName = " "; 

      Mock<IFileWrapper> mockFileWrapper = new Mock<IFileWrapper>(); 
      mockFileWrapper.Setup<string> (m => m.ReadTextFromFile (It.IsAny<string>())).Returns ("\n"); 
      mockFileWrapper.Setup (m => m.ReadTextFromFile (It.IsAny<string>())).Returns ("\n"); 

      //Act 
      charMatrix = initializeGrid.CreateCharMatrix (filePathName); 
      int countProvingAnAdditionalListHasBeenAdded = charMatrix.Count; 

      //Assert 
      Assert.AreEqual (2, countProvingAnAdditionalListHasBeenAdded); 
     } 
    } 

using System; 
using System.IO; 

namespace GameOfLife 
{ 
    public class FileWrapper : IFileWrapper 
    { 
     public string ReadTextFromFile(string path) 
     { 
      return File.ReadAllText (path); 
     } 
    } 
} 

using System; 

namespace GameOfLife 
{ 
    public interface IFileWrapper 
    { 
     string ReadTextFromFile(string filePathName); 
    } 
} 

看你的代码InitializeGrid依然采用FileWrapper类。它没有使用模拟类,因此代码仍在尝试使用文件系统。

您的InitializeGrid类需要使用IFileWrapper接口,而不是FileWrapper类。我会考虑将IFileWrapper接口传递给InitializeGrid类的构造函数。

public class InitializeGrid 
{ 
    IFileWrapper fileWrapper; 

    public InitializeGrid (IFileWrapper fileWrapper) 
    { 
     this.fileWrapper = fileWrapper; 
    } 

    public List<List<char>> CreateCharMatrix (string filePathName) 
    { 
     string inputGridTextFile = fileWrapper.ReadTextFromFile (filePathName); 
     // More code here... 
    } 
} 

在您的测试,你会通过将mockFileWrapper.Object给它的构造使用嘲笑IFileWrapper构建InitializeGrid对象。

 List<List<char>> charMatrix; 
     string filePathName = " "; 

     Mock<IFileWrapper> mockFileWrapper = new Mock<IFileWrapper>(); 
     mockFileWrapper.Setup<string> (m => m.ReadTextFromFile (It.IsAny<string>())).Returns ("\n"); 
     mockFileWrapper.Setup (m => m.ReadTextFromFile (It.IsAny<string>())).Returns ("\n"); 

     InitializeGrid initializeGrid = new InitializeGrid (mockFileWrapper.Object); 
+0

非常感谢!只需输入此代码,我的测试已通过哈哈。 –