I'm building a Sudoku generator. I have a board class with a number of methods:
public class Board {
public Board() { /* Creates an empty board */ }
public bool ValidateRow(int row) { /* Checks for errors in row */ }
public bool ValidateColumn(int column) { /* Checks for errors in column */ }
...
}
I'm following TDD, and as such have a full suite of tests for all these methods. I would like to add two new methods to this class:
public static Board GenerateFilled() { /* Creates a solved board /* }
public bool ValidateBoard() { /* Checks for any error on the board /* }
I'm struggling with how to write my tests for these methods. My first thought was:
[TestMethod]
public void GenerateFilled_GeneratesAValidSolvedBoard() {
var board = Board.GenerateFilled();
Assert.IsTrue(board.ValidateBoard());
}
but I realized I wrote the same test for ValidateBoard:
[TestMethod]
public void ValidateBoard_NoErrors_ReturnsTrue() {
var board = Board.GenerateFilled();
Assert.IsTrue(board.ValidateBoard());
}
This test relies on both GenerateFilled and ValidateBoard working correctly, though the method under test changes. I've come up with the following ways to avoid this problem:
- Duplicate the logic of the method not under test into the test. Use that logic to validate my method under test instead of calling the other method.
- Leave the GenerateFilled test as is and use hardcoded sample data to test ValidateBoard instead of calling GenerateFilled.
I'm not a fan of option 1 because it will make keeping the tests accurate tedious if small changes to the logic in the methods that were duplicated change.
I don't really like option 2 either though because it relies on my sample data case being general of all cases, which is less likely to be true the larger the dataset is.
I suppose in the worst case this just results in any error in either method causing both tests to fail, but it is a bit of a smell. Has anyone come across a similar scenario and found a better solution than the two above?
Board.GenerateFilled()
in the shown way, this will be just one sample data case as well.