- Software Testing with TDD ~ Karl P.
- Create a new node.js project
- Install and configure a local copy of eslint. Don't install it globally for reasons I mentioned before.
- Install mocha as a dev dependency
- Install chai as a dev dependency
- Create a test directory
- Create a test.js inside that test directory
- Create a multiply.js
- Update npm script to run tests using mocha
- Red: Write your first test: assert.equal(multiply(1, 1), 1); in ./test/test.js
- Red: Run the test. Did it pass? No! Becasue you haven't written the multiply function yet!
Get the test to pass, by writing a multiply function, that is just enough code to pass the test, i.e. function multiply (a, b) { return 1;} 4. Run the test? Does it pass? Should do! Now we are green! 5. Refactor? Probably not necessary!
- Red. Write the next test. Run the tests: assertEqual(multiply(2, 2),4)
- Green: Update multiply() to pass the second test. Make sure the first test still passes too!
- Do I need to refactor? Nope, its pretty simple
- Red. Write the next test: assertEqual(3, 3), 9)
- Red/Green: Does it pass, maybe, depends on your solution.
- Green: Update multiply() to pass the third test, if you need to. Make sure you didnt break test 1 or test 2! If you did, fix them.
- Do I need to refactor? Nope, its pretty simple
- Red. assertEqual(4, 4), 16)
- Oh! Its green!
- Red: assertEqual(23, 45), 23 * 45)
- Oh! Its till green!
- What might you do if your language didn't natively support multiply?