Randomized tests
A bad example
The following example is taken from a tris kata made with Javascript and using Jest framework. In this tests I want to show how is possible to check that Tris class know that after one move remain 8 other moves.
test('... remember remaining tiles', () => {
var t = new Tris();
t.addMove(42);
expect(t.remainingTiles()).toEqual(8);
});
Here a bad example of how this test can be recycled.
test('... remember remaining tiles', () => {
var t = new Tris();
t.addMove(42);
expect(t.remainingTiles()).toEqual(8);
t.addMove(23);
expect(t.remainingTiles()).toEqual(7);
});
A good example
Next example show how code works with a range of inputs at random. In this test is not possible to know how many moves will be added. This test guarantee that code works any time.
test('... remember remaining tiles', () => {
var t = new Tris();
var max = 9;
var min = 1;
var rnd = Math.floor(Math.random() * (max - min + 1)) + min;
for (var i = 1; i <= rnd; i++) {
t.addMove(i);
}
expect(t.remainingTiles()).toEqual(9 - rnd);
});