unit testing in javascript: introducing jasmine
DESCRIPTION
by Violeta Dabnishka, Software Architect [email protected] blog: http://backendprogrammer.blogspot.caTRANSCRIPT
Unit testing in Javascript Introducing Jasmine
About me
Violeta Dabnishka, Software Architect
Blog: http://backendprogrammer.blogspot.ca
Slide 3
What is Jasmine?
BDD framework for testing Javascript
Lightweight, without external dependencies
Intuitive syntax
Does not require a DOM
Browser agnostic
Widely adopted
Configurable
Can be included in CI
Start here: http://pivotal.github.com/jasmine/
Testing playground: http://tryjasmine.com/
Slide 4
Fixtures
describe()-ing features (or entities)
beforeEach() and afterEach()
Nesting features – it is MORE than nesting classes
Disabling: xdescribe()
Slide 5
Scenarios
A scenario is a different aspect of a feature
it() is observable
it() has to meet expect()-ations
expect().toBe()
expect().toEqual()
Slide 6
The Game of Life
Cellular automaton created by John Conway
Infinite discrete two-dimensional grid
Each cell on the grid is either dead or alive
Rules govern what the next generation will be
A living cell with less than 2 living neighbours dies of loneliness
A living cell with more than 3 living neighbours dies of over-crowding
A dead cell with exactly 3 living neighbours becomes alive (reproduction)
Rules are applied simultaneously to each cell to produce the next generation
Slide 7
Test-driving the Game of Life – Cells
A cell is dead by default
A cell retains the state when passed as constructor param
A cell should be observable (how else are going to test it?)
Slide 8
Test-driving the Game of Life – the Plane
Go simple – our plane has dimensions, pass them as params
Cells are objects, and we want to avoid NullReference exceptions (or whatever that thing is
called in Javascript) – have the Plane to create default ones for us
What about initial conditions – our Plane should initialize its dimensions accordingly
More about initial conditions – creating cells
Plane knows who’s dead and who’s alive – have it count the alive neighbours
Hmm, we have walls and corners, deal with it!
Slide 9
Test-driving the Game of Life – God
God knows the current population – but we have to tell it what it is
God decides who live and who dies
Hey, this plane initialization stuff is tricky and ugly and prone to errors – can’t we just mock
it? Yes, we can!
Looks OK but even with mocking there is a lot of duplication. What do we do?
Q & A
What did you think?