其中是单元和集成测试之间的线(示例)
本文关键字:示例 之间 集成测试 单元 | 更新日期: 2023-09-27 18:28:54
我在理解这两种测试之间的线在哪里(或应该在哪里)时遇到了问题。所以,我有一个伪例子:一个简单的井字游戏。游戏有自己的棋盘(3x3),有9个单元格。依赖关系很明显:游戏<-董事会<-单元格。
快速描述:每次用户点击"新游戏",新的游戏就会被创建(由GameFactory创建),板需要被清除/重置。在GameFactory的整个生命周期中,只有一个Board对象。重置Board意味着创建全新的Cell对象。这是代码:
游戏工厂:(正如你所看到的,我创建了一次Board,所以我需要在游戏构造函数中重置它):
public class GameFactory : IGameFactory
{
private readonly IBoard board;
public GameFactory(IBoard board)
{
this.board = board;
}
public IGame Create()
{
return new Game(board);
}
}
Cell和CellFactory
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单元测试?我可以说,如果一个对象依赖于其他对象(即使它们是接口),那么它就必须是集成测试,而不是单元测试吗?
这是我已经做的测试。(在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));
}
}
}
编辑:板构造函数:
public Board(int width, int height, ICellFactory cellFactory)
{
Width = width;
Height = height;
this.cellFactory = cellFactory;
Reset();
}
第二版:现在是我的整个测试。它通过:
[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));
}
}
}
}
您可以为CellFactory
和Cell
使用一个简单的存根,例如使用NSubstitute。在这种情况下,您将只测试Reset
方法逻辑
[Test]
public void BoardCreationTest()
{
var board = CreateBoard();
//Calling method explicitly makes test more readable
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));
}
}
}
private IBoard CreateBoard()
{
var cellFactory = Substitute.For<ICellFactory>();
//You may replace Cell with another mock or smth else. Depends on your assert section.
cellFactory.Create(Arg.Any<int>(), Arg.Any<int>())
.Returns(x => new Cell((int)x[0], (int)x[1]));
IBoard board = new Board(3, 3, cellFactory);
}
Unit test
意味着您测试单个逻辑单元。因此,用fake替换所有依赖项,使您能够只测试一个逻辑单元。