dimanche 21 décembre 2014

Unit testing angularjs controller using Node.js, Gulp.js and Mocha

I am trying to unit test an angularjs controller using Node.js. I am using gulp.js and mocha to run the tests, via gulp-mocha.


This is what my gulpfile.js looks like right now:



(function () {

var gulp = require('gulp');
var mocha = require('gulp-mocha');

gulp.task('default', function () {
return gulp
.src('Scripts/Tests/*.js', { read: false })
.pipe(mocha({
ui: 'tdd',
reporter: 'dot',
globals: {
angular: require('./Scripts/angular.js')
}
}));
});

})();


This is the code under test:



(function () {

var application = angular.module('Main');

application.controller('MainController', ['$scope', function ($scope) {

$scope.isDisabled = function () {
return true;
};

}]);

})();


And this is the test file itself:



(function () {

var chai = require('chai');
var assert = chai.assert;

angular.module('Main', []); // Create an empty module
require('../MainController.js'); // Fill the module with the controller

suite('Main Controller', function () {

test('isDisabled always true', function () {

var controllerFactory = angular.injector.get('$controller');
var controller = controllerFactory('MainController', {
'MainController': {}
});

var result = controller.isDisabled();
assert.isTrue(result);
});

});

})();


I need to make angular a global in order for my test and the file I am testing to work. However, the call to require in gulpfile.js is giving me a Reference Error: window is not defined error. That makes sense, since I am running in Node.js.


What do I need to do to load up angular in Node.js?


Aucun commentaire:

Enregistrer un commentaire