1
在我的單元測試中,我需要改變之前被嘲笑的對象的值。例如:如何更改被嘲笑的對象?
public class Cell
{
public int X { get; set; }
public int Y { get; set; }
public string Value { get; set; }
}
public class Table
{
private Cell[,] Cells { get; }
public Table(Cell[,] cells)
{
Cells = cells;
}
public void SetCell(int x, int y, string value)
{
Cells[x, y].Value = value;
}
}
我想在Table
測試SetCell
方法。因此,首先我模擬Cell
,然後我創建一個Cell[,]
單元格陣列,創建一個Table
作爲參數傳遞單元格數組。
SetCell
不起作用,因爲(我認爲)我不能改變之前被嘲笑的對象。我該如何改變它?
這裏是我的測試:
ICell[,] cells = new ICell[3, 4];
for (int i = 0; i < cells.GetLength(0); i++)
{
for (int j = 0; j < cells.GetLength(1); j++)
{
var mock = new Mock<ICell>();
mock.Setup(m => m.X).Returns(i);
mock.Setup(m => m.Y).Returns(j);
mock.Setup(m => m.Value).Returns("");
cells[i, j] = mock.Object;
}
}
ITable table = new Table(cells);
table.SetCell(0, 0, "TEST"); // Cannot change it here :/