JavaScript Core 9
Test Driven Development (TDD)
- TDD
- BDD
- Let's Test
Test Driven Development (TDD)
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only. (Wikipedia)
A key principle in TDD is that we write think about our requirements before we dive into code: What should our program be able to do? What are the small components that make up a larger program?
We put this into practice by first writing a test for a piece of functionality, before we've even written any code. The test should first fail RED. We then write the code to make the test pass GREEN. At this point, we are safe to refactor the code (clean it up - remove duplication). The process is RED-GREEN-REFACTOR
Behavioural Driven Development (BDD)
You've already seen the structure of standard unit tests:
test('tick increases count to 1', function() {
var counter = new Counter();
counter.tick();
assert.equal(counter.count, 1);
});
While this test is correctly checking that the 'tick' method does indeed work, it makes assumptions about implementation details. For example, this test assumes that the Counter begins at 0. If we were to change something in the counter (maybe we want to start it at 10), then it will break the test.
With BDD, we focus on the behaviour of a particular unit of code, rather than the implementation details:
it('should increase count by 1 after calling tick', function() {
var counter = new Counter();
var expectedCount = counter.count + 1;
counter.tick();
assert.equal(counter.count, expectedCount);
});
As you can see, rather than worry about where the Counter has started, we are only concentrating on the behaviour of the tick method, namely that it increases the Counter by 1.
Let's Test
We'll be working on the code Bowling Kata today to practice TDD. We can kick off the first part together, then it will be left to you to continue the test driven development. Please work in pairs!
To start, clone the directory linked above, which will give you the basic file
setup. After cloning, npm install
and you'll be ready to go.
We'll be testing with Jest.
Resources
Homework
- Finish the Bowling Kata that we worked on today. If you have 'finished' it in class, continue refactoring it to clean up the code.
- Do another Kata (Test Driven is mandatory!) from http://kata-log.rocks/tdd.html - Suggestions (in order from easy to difficult) - Roman Numerals, Mars Rover, Tic Tac Toe. Your mentor can help you with this in a 1-1.
Project
Prepare for Next Class
Watch this Node.js/Express video (and ideally implement) - https://www.youtube.com/watch?annotation_id=annotation_2934154685&feature=iv&src_vid=ndKRjmA6WNA&v=FqMIyTH9wSg
Read https://github.com/node-girls/what-is-node and some of the listed resources in preparation for Node/Express next week.
If you have time, take a look at https://medium.com/javascript-scene/introduction-to-node-express-90c431f9e6fd for setting up an Express/node.js server. Don't worry if it is too complicated - this will be the focus of our next few classes.