I have a problem with unit testing AngularJS. The volume in unit test is undefined. But first, the source code:
Main angular module:
var app = angular.module('App', [
'AppCtrl',
'AppServices',
'AppDirectives',
'ngGrid',
'ui.bootstrap',
'angular-growl',
'ngAnimate'
]);
Controller module:
var appCtrl = angular.module('AppCtrl', []);
And finally, the controller for testing as a simplified version:
appCtrl.controller('SignalListCtrl', ['$scope',
function($scope) {
$scope.name = "World";
}]);
Now the test. Version 1:
describe("App", function () {
beforeEach(module("App"));
describe("SignalListCtrl", function () {
var scope, controller;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller("SignalListCtrl", {$scope: scope});
scope.$digest();
}));
it("should print World", function () {
expect(scope.name).toBe("World");
});
});
});
Error message: scope is undefined.
Version 2:
describe("App", function () {
beforeEach(module("App"));
describe("SignalListCtrl", function () {
var scope, controller;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller;
scope.$digest();
}));
it("should print World", function () {
controller("SignalListCtrl", {$scope: scope});
expect(scope.name).toBe("World");
});
});
});
Error message: controller is not a function.
I use karma if it depends on it.
This is karma.conf from angular -seed. These are all the libraries I used. I think that I do not need all the libraries for testing, so I commented on this. If everything is uncommented, the same error occurs. edit: Each lib I use is now located in karma.conf. I get the same error messages.
module.exports = function(config){
config.set({
basePath : '../',
files : [
'app/lib/jquery/jquery.js',
'app/lib/jquery/jquery-ui-dialog.js',
'app/lib/jquery/jquery-dialog.js',
'app/lib/angular/angular.js',
'app/lib/angular/angular-*.js',
'app/lib/angular/growl/*.js',
'app/lib/bootstrap/*.js',
'app/lib/highcharts/highcharts.js',
'app/lib/highcharts/export.js',
'app/lib/xml2json/*.js',
'app/js/*.js',
'test/lib/angular/angular-mocks.js',
'test/unit/*.js'
],
exclude : [
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Firefox'],
plugins : [
'karma-junit-reporter',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
})};
undefined? $rootScope. $New().
, - .
.
P.S: GoogleGroups → https://groups.google.com/forum/#!topic/angular/4oAEVLbEZT4
, , .