Uczę się pisać testy jednostkowe i napotkałem na problem. Mam dwie klasy pierwsza reprezentuje mapę gry, którą przekazuję do drugiej klasy która sprawdza czy nastąpiła wygrana. Chciałem przetestować jednostkowo klasę WinAlgorithm
ale nie wiem jak się za to zabrać przez to, że ta klasa jest zależna od Board
. Czy mógłbym prosić o wskazówki jak to ugryźć albo jeden test, który pokaże mi jak się coś takiego testuje?
public class Board
{
private const int DefaultSize = 3;
public Cell[,] Cells { get; }
public int BoardSize { get; }
public int MoveCount { get; private set; }
public Board(Options options)
{
Guard.ThrowIfNull(options, nameof(options));
BoardSize = options.BoardSize >= DefaultSize
? options.BoardSize
: DefaultSize;
Cells = new Cell[BoardSize, BoardSize];
CreateCells();
}
public Cell GetCell(int row, int col)
{
ThrowIfInvalid(row, col);
return Cells[row, col];
}
public void SetCell(int row, int col, Player player)
{
ThrowIfInvalid(row, col);
if (!IsEmpty(GetSell(row, col))
throw new InvalidOperationException();
Cells[row, col] = player == Player.Cross
? Cell.CreateCrossCell()
: Cell.CreateCircleCell();
MoveCount++;
}
public bool IsEmpty(Cell cell)
{
return cell.CellType == CellType.Empty;
}
// other methods
}
public class WinAlgorithm
{
private readonly Board _board;
public WinAlgorithm(Board board)
{
Guard.ThrowIfNull(board, nameof(board));
_board = board;
}
public bool IsRowWinner(int row)
{
var cell = _board.GetCell(row, 0);
if (_board.IsEmpty(cell))
return false;
for (var i = 1; i < _board.BoardSize; i++)
if (_board.GetCell(row, i) != cell)
return false;
return true;
}
public bool IsColWinner(int col)
{
var cell = _board.GetCell(0, col);
if (_board.IsEmpty(cell))
return false;
for (var i = 1; i < _board.BoardSize; i++)
if (_board.GetCell(i, col) != cell)
return false;
return true;
}
// other methods
}