<!DOCTYPE html>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.css" rel="stylesheet" />
<script data-main="main.js" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.17/require.min.js"></script>
</head>
<body>
<div class="container" style="margin-top:30px;">
<h1>AngularJS Unit Test Example using RequireJS, Mocha, Chai and Sinon</h1>
<div id="mocha"></div>
</div>
</body>
</html>
define(['simpleService'],
function() {
describe('SimpleService', function () {
// define variables for the services we want to access in tests
var SimpleService,
$log;
beforeEach(function () {
// load the module we want to test
module('app');
// inject the services we want to test
inject(function (_SimpleService_, _$log_) {
SimpleService = _SimpleService_;
$log = _$log_;
})
});
describe('#DoSomething', function () {
it('should log the message "something done!"', function () {
// Arrange
sinon.spy($log, 'info');
// Act
SimpleService.DoSomething();
// Assert
assert($log.info.calledOnce);
assert($log.info.calledWith('something done!'));
// Cleanup
$log.info.restore();
});
});
});
});
define(['angular'],
function(angular) {
return angular.module('app', []);
});
define(['app'],
function(app) {
app.factory('SimpleService', SimpleService);
SimpleService.$inject = ['$log'];
function SimpleService($log) {
var service = {
DoSomething: doSomething
};
return service;
function doSomething() {
$log.info('something done!');
}
}
});
require.config({
paths: {
angular: 'https://code.angularjs.org/1.4.0-beta.4/angular',
mocha: 'https://cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha',
chai: 'https://cdnjs.cloudflare.com/ajax/libs/chai/1.10.0/chai',
ngMock: 'https://code.angularjs.org/1.4.0-beta.4/angular-mocks',
sinon: 'http://sinonjs.org/releases/sinon-1.12.2'
},
shim: {
'angular': {
exports: 'angular'
},
'mocha': {
init: function(){
this.mocha.setup({
"ui": "bdd",
"reporter": "html"
});
return this.mocha;
}
},
'ngMock': {
deps: ['angular']
}
}
});
require(['mocha', 'chai', 'sinon', 'ngMock', 'simpleService.spec'],
function(mocha, chai){
mocha.run();
// add assert to global scope
window.assert = chai.assert;
});