unit testing in javascript: introducing jasmine

10
Unit testing in Javascript Introducing Jasmine

Upload: teksystems-montreal-solution-center

Post on 15-Jan-2015

234 views

Category:

Technology


0 download

DESCRIPTION

by Violeta Dabnishka, Software Architect [email protected] blog: http://backendprogrammer.blogspot.ca

TRANSCRIPT

Page 1: Unit testing in Javascript: Introducing Jasmine

Unit testing in Javascript Introducing Jasmine

Page 2: Unit testing in Javascript: Introducing Jasmine

About me

Violeta Dabnishka, Software Architect

[email protected]

Blog: http://backendprogrammer.blogspot.ca

Page 3: Unit testing in Javascript: Introducing Jasmine

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/

Page 4: Unit testing in Javascript: Introducing Jasmine

Slide 4

Fixtures

describe()-ing features (or entities)

beforeEach() and afterEach()

Nesting features – it is MORE than nesting classes

Disabling: xdescribe()

Page 5: Unit testing in Javascript: Introducing Jasmine

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()

Page 6: Unit testing in Javascript: Introducing Jasmine

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

Page 7: Unit testing in Javascript: Introducing Jasmine

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?)

Page 8: Unit testing in Javascript: Introducing Jasmine

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!

Page 9: Unit testing in Javascript: Introducing Jasmine

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?

Page 10: Unit testing in Javascript: Introducing Jasmine

Q & A

What did you think?