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. 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. In the entire lifetime of GameFactory there there is single object of 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):
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 is independent from the Game, but it has its own Cells and 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):
[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));
}
}
}
Aucun commentaire:
Enregistrer un commentaire