Learning testing in angular with Dave: https://daveceddia.com/testing-angular-part-1-karma-setup/
- Install
karma
and friends.$ npm install
. $ karma init
to generate karma's config filekarma.conf.js
. we usePhantomJS
as the browser to run the tests.- Install
angular
andangular-mocks
. Put their paths inkarma.conf.js
$ karma start
- Create the spect file
src/greeting/greeting.spec.js
- Inject app module and factory
- Create a local variable to be a reference to the factory
- Bring the app module in using
beforeEach(module(moduleName))
. - Inject the factory in another
beforeEach
. the injected factory has_
prefix and suffix. assign this to the variable from 2.1 - Write the spec in
it()
blocks.
https://daveceddia.com/testing-angular-part-2-jasmine-syntax/
-
describe
creates a group of tests. It is usually used to create a scenario/use case. -
beforeEach
runs before each test in its block. -
it
- a block of test -
expect
- assertion inside ofit
block. Should have 1 assertion in 1 test but in some cases it is OK to have more (manipulating data and then verify it). -
Matchers
toEqual
and friends. -
module
loads an angular module. used inbeforeEach()
-
inject
takes a function that will get injected by angular's DI. used to assign injected modules to local variables to be used with the tests.
https://daveceddia.com/testing-angular-part-3-testing-recipes/
- test pattern
- import the module/service
- inject the service (the one to test) in
beforeEach
. put it in a local variable to use with other tests. - write the tests
- try to keep one assertion per test to keep the method small. a method should do just one thing.