In meinem Komponententest muss ich den Wert des Objekts ändern, das zuvor gespottet wurde. Zum Beispiel:Wie ändere ich ein Objekt, das verspottet wurde?
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;
}
}
Ich möchte SetCell
Methode in Table
testen.
Also, zuerst mache ich Mock Cell
, dann erstelle ich ein Cell[,]
Array von Zellen, erstellen Sie eine Table
übergeben Array von Zellen als Parameter.
SetCell
funktioniert nicht, weil (ich denke) Ich kann das Objekt nicht ändern, das zuvor verspottet wurde. Wie kann ich es ändern?
Hier ist mein Test:
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 :/