C# 如何更改被模拟的对象?
在我的单元测试中,我需要更改之前模拟的对象的值。例如:C# 如何更改被模拟的对象?,c#,.net,unit-testing,testing,moq,C#,.net,Unit Testing,Testing,Moq,在我的单元测试中,我需要更改之前模拟的对象的值。例如: 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) {
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
方法
因此,首先我模拟单元格
,然后我创建一个单元格[,]
单元格数组,创建一个表格
将单元格数组作为参数传递
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 :/
ICell[,]单元=新的ICell[3,4];
for(int i=0;im.X).Returns(i);
mock.Setup(m=>m.Y).Returns(j);
Setup(m=>m.Value)。返回(“”);
单元[i,j]=模拟对象;
}
}
ITable表格=新表格(单元格);
表.SetCell(0,0,“测试”);//无法在此处更改它:/
以便可以更新它们
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.SetupAllProperties();
mock.Object.X = i;
mock.Object.Y = j;
mock.Object.Value = "";
cells[i, j] = mock.Object;
}
}
//...other code removed for brevity
ICell[,]单元=新的ICell[3,4];
for(int i=0;i