【发布时间】:2016-04-07 15:15:04
【问题描述】:
我很难理解这两种测试之间的界限在哪里(或应该在哪里)。所以,我有一个虚拟的例子:一个简单的井字游戏。游戏有自己的棋盘 (3x3),有 9 个单元格。依赖性很明显:Game .
简要说明:每次用户点击“新游戏”,新的游戏都会被创建(由GameFactory)并且需要Board清除/重置。在 GameFactory 的整个生命周期中,只有一个 Board 对象。重置 Board 意味着创建全新的 Cell 对象。代码如下:
GameFactory:(如您所见,我创建了一次 Board,所以我需要在 Game 构造函数中重置它):
public class GameFactory : IGameFactory
{
private readonly IBoard board;
public GameFactory(IBoard board)
{
this.board = board;
}
public IGame Create()
{
return new Game(board);
}
}
细胞和细胞工厂
public interface ICell
{
int PlayerId { get; set; }
int Row { get; set; }
int Column { get; set; }
}
public interface ICellFactory
{
ICell Create(int row, int column);
}
public class CellFactory : ICellFactory
{
public ICell Create(int row, int column)
{
return new Cell(row, column);
}
}
最后,董事会:
public interface IBoard
{
int Width { get; }
int Height { get; }
void Reset();
// Rest is not important for that question
// ...
}
public class Board : IBoard
{
private ICell[,] cells;
private readonly ICellFactory cellFactory;
public int Width { get; }
public int Height { get; }
public void Reset()
{
cells = new ICell[Height, Width];
for (int i = 0; i < cells.GetLength(0); i++)
{
for (int j = 0; j < cells.GetLength(1); j++)
{
cells[i, j] = cellFactory.Create(i, j);
}
}
}
// Rest is not important for that question
// ...
}
问题:如何在 Board 对象中测试 Reset 方法? Board 独立于游戏,但它有自己的 Cells 和 CellFactory。
一些其他相关问题: - 是否可以创建板单元测试?我可以这样说,如果一个对象依赖于其他对象(即使它们是接口),那么它已经必须是集成测试,而不是单元测试?
这是我已经做过的测试。 (在 Board 构造函数中调用了 Reset):
[Test]
public void BoardCreationTest()
{
var cellFactory = new CellFactory();
IBoard board = new Board(3, 3, cellFactory);
for (int i = 0; i < board.Height; i++)
{
for (int j = 0; j < board.Width; j++)
{
// Check if board.cells[i,j].PlayerId is zero (it has to be zero, player zero is empty cell)
// Another thing is that cells are private, cause project doesn't need it public
// Should I make cells public just for tests?
// Right now I'm checking it IsMoveValid(column, row, playerId)
// It has to be true, when player 1 wants move in certain cell (it has to be zero, player zero is empty cell)
Assert.IsTrue(board.IsMoveValid(i, j, 1));
}
}
}
编辑:Board 构造函数:
public Board(int width, int height, ICellFactory cellFactory)
{
Width = width;
Height = height;
this.cellFactory = cellFactory;
Reset();
}
EDIT2:我现在的整个测试。它通过了:
[TestFixture]
class BoardTests
{
private IBoard board;
[SetUp]
public void RunBeforeAnyTests()
{
var cellFact = Substitute.For<ICellFactory>();
cellFact.Create(Arg.Any<int>(), Arg.Any<int>())
.Returns(x => new Cell((int)x[0], (int)x[1]));
board = new Board(3, 3, cellFact);
}
[Test]
public void BoardCreationTest()
{
board.Reset();
for (int i = 0; i < board.Height; i++)
{
for (int j = 0; j < board.Width; j++)
{
Assert.IsTrue(board.IsMoveValid(i, j, 1));
}
}
}
}
【问题讨论】:
-
Board 可以进行单元测试,具体取决于它如何获取 ICell 和 ICellFactory 的实例。考虑能够注入它们以允许更轻松的对象模拟。
-
cellFactory和cells的实例可以通过构造函数注入就足够了。这使您有可能通过这些对象的“测试”实现并测试Reset方法的行为。 -
@Fabio 我将 cellFactory 注入到 Board 构造函数中,但是 ICell 呢?我不注入它们,因为我有 cellFactory - 所以我在需要它们时创建它们。我应该如何模拟 cellFactory?
-
@mbm 我编辑了我的问题并添加了 Board 构造函数。问题是,它是一个工厂。注入 ICell 没有意义。我注入 ICellFactory 然后用它来创建单元格(准确地说是 9 个不同的单元格,但可能有 10x10 的 Board,所以有 100 个不同的单元格!!)。问题仍然存在,我如何模拟 Board 和 CellFactry,以便使用最佳实践编写测试
标签: c# .net unit-testing testing integration-testing