Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改被模拟的对象?_C#_.net_Unit Testing_Testing_Moq - Fatal编程技术网

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