[英]Where is the line between unit and integration test (on example)?
I have a problem in understanding where is (or should be) a line between those two kind of tests. 在理解这两种测试之间的界限在哪里(或应该在哪里)时,我遇到了一个问题。 So, I have a dummy example: A simple tic-tac-toe game.
因此,我有一个虚拟的例子:一个简单的井字游戏。 The game has its own board (3x3) with 9 cells.
游戏拥有自己的带有9个单元的棋盘(3x3)。 Dependencies are obvious: Game <- Board <- Cell .
依赖性很明显: 游戏<-棋盘<-单元格 。
Quick description: Every time user click "New Game" , the new Game is created (by GameFactory) and Board needs to be cleared/reset. 简单描述:每次用户单击“新建游戏” ,都会创建新游戏 (由GameFactory创建),并且需要清除/重置棋盘 。 In the entire lifetime of GameFactory there there is single object of Board.
在GameFactory的整个生命周期中,只有一个对象是Board。 Reset the Board means to create brand new Cell objects.
重置开发板意味着创建全新的单元对象。 Here is the code:
这是代码:
GameFactory: (as you can see I created Board once, so I need to reset it in Game constructor): 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);
}
}
Cell and 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);
}
}
and finally, Board : 最后, 董事会 :
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
// ...
}
The Question: How can I test Reset method in Board object? 问题:如何在Board对象中测试Reset方法? Board is independent from the Game, but it has its own Cells and CellFactory.
董事会独立于游戏,但它拥有自己的Cells和CellFactory。
Few additional questions related: - Is it possible to create Board unit tests? 很少有其他相关问题:-是否可以创建董事会单元测试? Can I say that, if an object depends on other objects (even if they are interfaces) then it's already has to be integration test, not unit test?
我可以说,如果一个对象依赖于其他对象(即使它们是接口),那么它已经必须进行集成测试,而不是单元测试?
Here is test I made already. 这是我已经做过的测试。 (Reset is called in Board constructor):
(在Board构造函数中调用了复位):
[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));
}
}
}
EDIT: Board constructor: 编辑: 板构造函数:
public Board(int width, int height, ICellFactory cellFactory)
{
Width = width;
Height = height;
this.cellFactory = cellFactory;
Reset();
}
EDIT2: My whole test now. EDIT2:现在我的整个测试。 It passes:
它通过:
[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));
}
}
}
}
You can use a simple stub for CellFactory
and Cell
, using NSubstitute for example. 您可以对
CellFactory
和Cell
使用简单的存根,例如使用NSubstitute 。 In this case you will test only Reset
method logic 在这种情况下,您将只测试
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
mean that you test a single unit of logic. Unit test
意味着您测试一个逻辑单元。 So replacing all dependencies with fakes, gives you ability to test only one unit of logic. 因此,将所有依赖项替换为伪造品,使您能够仅测试一个逻辑单元。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.