Unit testing can be describe as:
For every automatic action that occurs on controller initialization, or any controller method invoked in scope, test the following:
- Given correct input, does it call the expected external methods/query the expected external resources.
- Given correct results from all external methods/resources, does it generate the expected response.
If required by design:
- Given incorrect input, does it refuse the input with useful errors.
- Given incorrect results from external methods/resources, does it generate useful errors.
It should be immediately obvious what a test is testing, and why. If it isn't obvious, comments explaining/justifying the test assertions should be added.
When unit testing, we want to test at the smallest level possible.
Docs | Help | Keywords |
---|---|---|
ngMock | ngMock.md | module , inject , $controller , $httpBackend |
Sinon | sinon.md | stub , spy , called |
Chai | chai.md | expect , to , equal , be |
Sinon-chai | N/A | to.have.been.called (allows better chaining) |
Mocha | mocha.md | describe , it , before , beforeEach , after , afterEach |
Karma | N/A | Simply runs the tests |
Credit to Sandi Metz from her talk at RailsConf 2013.