So I made the following test for a class Board that would to be born:
[TestMethod]
public void Set_The_Origin_As_Violet_And_The_Query_Confirms_It() {
Board board = new Board(10, 10);
Color expected = Color.Violet;
board.SetColorAt(0, 0, expected);
Color actual = board.GetColorAt(0, 0);
Assert.AreEqual(expected, actual);
}
I tried running the code, but the compiler signaled that Board didn’t exist. So I created it.
I tried to run the code again, but it was of no avail, as both SetColorAt() and GetColorAt() methods didn’t exist. I created them:
public void SetColorAt(int x, int y, Color color) {
}
public void GetColorAt(int x, int y) {
}
Still, not everything was fine, as I had to return Color.Violet. So I changed GetColorAt() to
public void GetColorAt(int x, int y) {
return Color.Violet;
}
So I got a green light for the first time.
What I want my final code to be on the class board is something of the form:
public class Board
{
private Color[,] board;
public Board(int x, int y)
{
board = new Color[x, y];
}
public void SetColorAt(int x, int y, Color color) {
board[x, y] = color;
}
public Color GetColorAt(int x, int y) {
return board[x, y];
}
}
My first question is….
how to get there? Can I consider that in the “refactor” phase of the Unit-Test I have shown above, when removing duplication I will eventually end up with this code?
If the answer is yes, I feel that my Unit-test is testing something way too “localized” for what the code actually does. You see, the test is just checking for 1 pixel and one color, while the code itself is way more complex and rich.
Maybe the solution would be to add more Unit-Tests? Which would you advise to make?
My second question is…
I know later I’ll want to have an IBoard. Should I just express that in the above Unit-Test? Should I let
Board board = new Board(10, 10);
as it is and still create the IBoard interface? How to deal with this?
The return Color.Violet statement here can be considered Data duplication, so you can refactor this bit out. The simplest thing that could possible work though would be having a single Color value that gets set when you call SetColorAt
Now, you’ll need more tests, to show you can set seperate cells to different colors.