There’s probably easier ways to do this (using angular-seed as a starting point?) but I like to get an understanding of what it takes to do something from scratch, so here’s some notes on adding karma and jasmine to an existing AngularJS project (leave me a comment with suggestions or corrections). My references to getting this working:
- AngularJS developer guide / Unit Testing
- Testing With AngularJS Part 1: Setting up Unit Testing With Karma
Assuming you already have node.js, use npm to install the karma and dependencies:
sudo npm install -g karma
sudo npm install -g karma-jasmine
sudo npm install -g karma-chrome-launcher
sudo npm install -g karma-cli
Karma config file – in the files section, list all dependent libraries, controllers being tested, and path/pattern to the test js to be executed:
[code language=”javascript”]
module.exports = function (config) {
config.set({
basePath: ‘.’,
files: [
‘app/bower_components/angular/angular.min.js’,
‘app/bower_components/angular-mocks/angular-mocks.js’,
‘app/js/controller2.js’,
‘test/*.js’
],
exclude: [
],
autoWatch: true,
frameworks: [
‘jasmine’
],
browsers: [
‘Chrome’
],
plugins: [
‘karma-chrome-launcher’,
‘karma-jasmine’
]
});
};[/code]
Starting karma:
karma start karma.conf.js
Example skeleton Jasmine test:
[code language=”javascript”]
describe(‘ExmapleCtrl’, function() {
beforeEach(module(‘ExampleApp’));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
describe(‘test suite name’, function() {
it(‘description of test’, function() {
var $scope = {};
var controller = $controller(‘ExampleCtrl’, { $scope: $scope });
//setup values
$scope.somevalue = 1;
//call method on controller
$scope.examplemethod();
expect(someValue).toEqual(exampleValue));
});
});
});
[/code]