Skip to content

Instantly share code, notes, and snippets.

@amhed
Created November 19, 2013 15:47
Show Gist options
  • Save amhed/7547399 to your computer and use it in GitHub Desktop.
Save amhed/7547399 to your computer and use it in GitHub Desktop.
Example of TDD with NodeJS
//This uses mocha + should.js for running the unit tests
it('should allow players to take out a Pawn when a five(5/x) is rolled', function() {
//Since the dice throws random numnbers, we either need to throw it many times until
//we get the value we want, or just overwrite it's functionality.
//Here we chose to overwrite using the sinonJS mocking framework
sinon.stub(game, 'throwDices').returns([5,4]);
sinon.stub(game, 'lastDiceThrow').returns([5,4]);
game.enterPawn(0);
game.spaces[5].pawns.length.should.be.above(0);
});
//But I could have just done:
it('should allow players to take out a Pawn when a five(5/x) is rolled', function() {
//instead of using a mocking framework I could just overwrite the original
//methods on the game object
game.throwDices = function() {return [5,4]};
game.lastDiceThrow = function() {return [5,4]};
game.enterPawn(0);
game.spaces[5].pawns.length.should.be.above(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment