This post enlighten by Jasmine JavaScript Testing
unit test
a piece of code tests a functionality of unit of application code.
Jasmine
Jasmine: behavior-driven development test framework
TDD vs. BDD
test-driven development
turns into behavior-driven development
by Dan North
BDD
- Given: this provides an initial context
- When: this defines the event occurs
- Then: this ensures an outcome
Jasmine BDD example
|
|
spec
each unit test call spec
, short for specification
write a Jasmine test
describe
function is a global
Jasmine function, used to define test contest, it creates a new test suite(a collection of test cases), acceptes two params:
string
: name of test suitefunction
: a function that will contains all its specs
|
|
it
, another golbal
Jasmine function, accepts two params:
string
: title of the specfunction
: a function that will contains the spec code
|
|
expect
, an assertion or expectation is a comparison between two values, result of comparison is true means success, along with a matcher
that indicates what comparison must be made with the values.
|
|
Setup and teardown
beforeEach
: setup function, runs from outside before every spec(it
)- run spec(
it
) afterEach
: teardown function, runs from outside after every spec(it
)