[英]How do I mock a text output from reading a file in C# using the Moq Framework on Monodevelop
這個周末我一直在頭。 基本上,我正在為《生命游戲》編寫代碼kata,它涉及讀取文本文件。 我將輸入一個文本文件,該文件包含網格的二維表示並將所有點存儲在列表的列表中。 我試圖將從文件中獲得的文本輸入模擬為\\\\ n新行,以便我可以編寫單元測試以檢查是否在列表列表中創建了新列表。 我創建了一個文件包裝程序來處理文本文件的讀取,這就是我要模擬的內容。 代碼符合要求,但測試失敗,並顯示錯誤消息“ System.ArgumentException:指定的路徑不是合法形式”。 似乎仍然期望文件路徑,但是模擬應該更改此行為,對嗎? 任何幫助,將不勝感激。
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...
}
}
在您的測試中,您可以通過將模擬文件FileFilerr.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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.