I'm writing a small game in C# using Test Driven Development approach. I'm facing a problem:
I have a class CheckersBoard
:
public class CheckersBoard
{
private const string InitialBoardPattern = "B-B-B-B-" +
"-B-B-B-B" +
"B-B-B-B-" +
"--------" +
"--------" +
"-W-W-W-W" +
"W-W-W-W-" +
"-W-W-W-W";
public SquareStatus[,] Squares { get; private set; }
public CheckersBoard(IBoardGenerator boardGenerator)
{
Squares = boardGenerator.GenerateFromPattern(InitialBoardPattern, 8, 8);
}
//....
}
And a class BoardGenerator
:
public class BoardGenerator : IBoardGenerator
{
public SquareStatus[,] GenerateFromPattern(string pattern, int width, int height)
{
//....
}
}
BoardGenerator
allows me to initialize CheckersBoard
in a very readable way. So, I really would like to use BoardGenerator
in my unit tests to avoid ugly array initialization.
But it's against the rule that says that I must keep all my unit tests independent of each other. If the test of GenerateFromPattern
fails, that will produce a "cascade" effect and all the tests which use GenerateFromPattern
will fail too.
This is the first time I have ever used unit tests so I'm a little bit confused. How can I avoid this problem? Is there something wrong with my design?