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

How do I mock a text output from reading a file in C# using the Moq Framework on Monodevelop

我整个周末都在为这个问题苦思冥想。基本上我正在为 Game of Life 做代码套路,它涉及读取文本文件。我接受了那个包含网格的二维表示的文本文件,并将所有点存储在列表的列表中。我正在尝试模拟从文件中获取的文本输入只是“\n”一个新行,这样我就可以编写单元测试来检查是否在列表列表中创建了一个新列表。我创建了一个文件包装器来处理文本文件的读取,这就是我想要模拟的。代码符合要求,但测试失败并显示错误消息 "System.ArgumentException : The specified path is not of a legal form"。它似乎仍然需要一个文件路径,但模拟应该改变这种行为吧?任何帮助,将不胜感激。


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 class。它没有使用模拟 class,因此代码仍在尝试使用文件系统。

您的 InitializeGrid class 需要使用 IFileWrapper 接口而不是 FileWrapper class。我会考虑将 IFileWrapper 接口传递给 InitializeGrid class.

的构造函数
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);