Using mocks with Jasmine tests for AngularJS

I’m not entirely sure I understand exactly what is going on here or how this is working, but posting to refer back to later.

I have a controller that’s using $interval. Rather than coding a delay into my test in order to let time pass in real time before I do my assertions, a better approach would be to mock out $interval so you can move elapsed time forward programmatically, which is a much better approach.

Luckily the ngMock package provides mocks for most things, including $interval. To move time, you call $interval.flush(millis);

[code language=”javascript”]

describe(‘Test variable date timer’, function () {

it(‘Runs for 1 iteration, 30min interval, adds 30 mins’, function () {
var $scope = {};
angular.mock.inject(function ($controller, _$interval_) {
$interval = _$interval_;
controller = $controller(‘VariableRateDateCtrl’, {$scope: $scope, $interval: $interval});

});

$scope.iterations = 1;
$scope.timeInterval = 30;
$scope.date = moment();
startDate = $scope.date.clone();

$scope.start();
$interval.flush(1000 * $scope.iterations);

endDate = $scope.date;
expectedEndDate = startDate.clone().add($scope.timeInterval, ‘minutes’);
expect(endDate.isAfter(startDate)).toEqual(true);
expect(endDate).toEqual(expectedEndDate);
});[/code]

Setting up Karma and Jasmine unit testing for an AngularJS app

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:

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]