I am currently working on testing a controller that listens for an event to perform certain actions. The event handler relies on a crucial global variable named Main
. Despite my efforts to mock all dependencies in the unit test, an error is being thrown by Karma stating:
ReferenceError: Main is not defined
Below is the Angular code snippet :
App.controller('AppController', ['$scope', function($scope){
$scope.$on('$viewContentLoaded', function() {
Main.initComponents(); // initialize core components
});
}]);
Here is the content of the spec file:
describe("App", function () {
var scope, AppController, Main;
describe("Login", function () {
beforeEach(module("App"));
beforeEach(inject(['$rootScope', '$controller', function($rootScope, $controller){
scope = $rootScope.$new();
Main = jasmine.createSpyObj("Main",["initComponents"]);
AppController = $controller('AppController', {$scope : scope});
}]));
it("should call initComponents() on Main module upon receiving $viewContentLoaded event", inject(['$rootScope', function ($rootScope) {
$rootScope.$broadcast('$viewContentLoaded');
expect(Main.initComponents).toHaveBeenCalled();
}]));
});
});
And here's the configuration from karma.conf.js:
module.exports = function(config){
config.set({
basePath : './',
files : [
'app/bower_components/angular/angular.js',
'app/bower_components/oclazyload/dist/oclazyLoad.min.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.min.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/**/app.js',
'app/**/controllers.js',
'app/**/services.js',
'app/**/directives.js',
'app/**/filters.js',
'app/**/routes.js',
'app/**/specs.js'
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-junit-reporter'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
});
};
Can anyone please assist me with identifying the issue? Thank you.