We all know we need automated tests for our code - they are our guardian angel against regression bugs. But what exactly is the point in writing a test in advance - how does this even make sense? The first thing to grasp is that, at this stage, these are not tests - but specifications, which force us to think about exactly what we want to do - in advance. In addition TDD/BDD provide a rigorous methodology, which can help to keep us sane whilst developing complex code in steady confident steps.
3. 16/01/18JS OXFORD
A Simple Unit Test
class Calculator {
sum(a, b) {
return a + b;
}
}
let calc = new Calculator();
let result = calc.sum(1, 2);
assert.equal(result, 3);
Class Unit Test
5. [test]
A procedure intended to
establish the performance,
or reliability of something,
especially before it is taken
into widespread use.
To do something in order
to discover if something is
safe, works correctly, etc.,
or if something is present
Noun Verb
9. MAKE IT
PASS
REFACTOR
WRITE
FAILING
TEST
3 RULES OF TEST DRIVEN DESIGN
1. No code unless you’re making a test pass
2. Stop writing the test as soon as it fails
3. Write only enough code to make it green
21. Test Doubles
- Fake
- Stand in for a class/function that does not exist yet
- Stub
- Fake that returns fixed data (indirect outputs)
- Mock
- Stub that can capture the indirect outputs for later observation
- Spy
- Spy on method calls and capture inputs with expectations
[Meszaros 2007]
23. Stub declaration
Declaration Effect
spyOn(obj,'method').and.returnValue(value) Will cause value to be returned
spyOn(obj,'method').and.returnValues(val1, val2) Return values in order of
calling
spyOn(obj,'method').and.throwError('error') Will cause exception with
string ‘error’
spyOn(obj,'method’).and.callThrough() Will return the return value of
the original function
29. Don’t rush ahead with more
code.
Instead, add another example
and let it guide you to what you
have to do next.
- David Chelimsky 2010
A Language Shift
33. – Incremental and iterative development
– Short feedback loops
– Never write code without a failing test
Steve Freeman & Nat Pryce – 2009
A More Holistic Approach
34. TDD is described as
A Design Activity
“Start by writing an acceptance test” – Freeman & Pryce 2009
ATDD – Acceptance Test Driven
37. 16/01/18JS OXFORD
BDD – Dan North’s Definition
BDD is a second-generation, outside-in, pull-based,
multiple-stakeholder, multiple-scale, high-
automation, agile methodology.
38. 16/01/18JS OXFORD
BDD – Liz Keogh’s Definition
BDD is the art of using examples in conversation to
illustrate behaviour.
39. 16/01/18JS OXFORD
A Behavior Driven Approach?
Behavior Driven Development
Rules:
- Customers must be listed in the phone directory to register with Acme
Scenario: Customer cannot be registered if they are not in the phone directory
Given Jon is not listed in the Greater London telephone directory
When Jon tries to register for Acme Systems
Then Jon should be told they cannot register because they are not listed
45. You Don’t Need to Test the DOM
16/01/18JS OXFORD
module.exports = class Page {
constructor($element) {
this.$element = $element;
}
display(total) {
this.$element.animate({
borderWidth: 30
}, 1000).text(total);
}
};
46. You Don’t Need to Test the DOM
16/01/18JS OXFORD
const dom = new JSDOM("<html><div id='main'></div></html>");
$element = jQuery('#main');
spyOn($element, 'animate').and.returnValue($element);
let page = new Page($element);
page.display(total);
expect($element.animate).toHaveBeenCalledWith({borderRadius:
100});
49. Pros & Cons of TDD
¨ 100% test coverage (almost)
¨ Less fear of changing code
¨ Code guided (by tests)
¨ Loose coupling – high cohesion
¨ Instant feedback on code
¨ More testable code
¨ Living documentation
¨ Risky to change code
¨ Addition time spent upfront
¨ Can be less productive
¨ No assurance of optimal code
¨ Hard to adopt and master
¨ Test maintenance
17/01/18JS OXFORD
Pros Cons