2015년 7월 4일 토요일

angularjs unittest

0. prepare project for unitest
We need angular-mocks.js. You can download it from http://angularjs.org and https://code.angularjs.org/1.4.1/. I recommend to read the post before following this post if you are not familiar with angularjs and nodejs.

$ tree angularjs-unittest/
|____
| |____angular-animate.js
| |____angular-mocks.js
| |____angular-route.js
| |____angular-sanitize.js
| |____angular-touch.js
| |____angular.js
| |____bootstrap-theme.css
| |____bootstrap.css

1. configure Karma test-runner
You should configure karma per each project respectively . You can see the following questions.
$ karma init karma.config.js

karma.config.js is created.
|____angular-animate.js
|____angular-mocks.js
|____angular-route.js
|____angular-sanitize.js
|____angular-touch.js
|____angular.js
|____bootstrap-theme.css
|____bootstrap.css
|____karma.config.js

2. create unit test file in tests
|____angular-animate.js
|____angular-mocks.js
|____angular-route.js
|____angular-sanitize.js
|____angular-touch.js
|____angular.js
|____bootstrap-theme.css
|____bootstrap.css
|____karma.config.js
|____tests
| |____hellotest.js

3. start karma
$ karma start karma.config.js








We used jasmine testing framework. You may noticed that second test case is failed. Whenever you make changes, Karma automatically detect changes and run all unit test.