I want to test a controller where it listens on an event to do some stuff. The event handler is dependent on a global variable called Main. I'm trying to unit test the event listener by mocking all dependencies but Karma throws error :
ReferenceError: Main is not defined
Angular Code :
App.controller('AppController', ['$scope', function($scope){
$scope.$on('$viewContentLoaded', function() {
Main.initComponents(); // init core components
});
}]);
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 on $viewContentLoaded event", inject(['$rootScope', function ($rootScope) {
$rootScope.$broadcast('$viewContentLoaded');
expect(Main.initComponents).toHaveBeenCalled();
}]));
});
});
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'
}
});
};
What is the problem? Thanks for help.
Aucun commentaire:
Enregistrer un commentaire