vendredi 27 mars 2015

Jasmine, Angular "rootScope.$broadcast" test

I'm trying to write a test for a controller that has $rootScope.$on('accountsSet', function (event).... So in the tests I'm using .broadcast.andCallThrough() which many other questions here in SO suggest while it also worked before for me.


So my controller is pretty simple:


angular.module('controller.sidemenu', [])



.controller('SidemenuCtrl', function($rootScope, $scope, AccountsService) {

$rootScope.$on('accountsSet', function (event) {
$scope.accounts = AccountsService.getAccounts();
$scope.pro = AccountsService.getPro();
});

});


Any the test is simple as well:



describe("Testing the SidemenuCtrl.", function () {

var scope, createController, accountsService;

beforeEach(function(){

angular.mock.module('trevor');
angular.mock.module('templates');

inject(function ($injector, AccountsService) {

scope = $injector.get('$rootScope');
controller = $injector.get('$controller');
accountsService = AccountsService;

createController = function() {
return controller('SidemenuCtrl', {
'$scope' : $injector.get('$rootScope'),
'AccountsService' : accountsService,
});
};
});

});

it("Should load the SidemenuCtrl.", function () {

accountsService.setPro(true);

spyOn(scope, '$broadcast').andCallThrough();

var controller = createController();

scope.$broadcast("accountsSet", true);
expect(scope.pro).toBeTruthy();

});

});


The error I'm getting if for spyOn(scope, '$broadcast').andCallThrough();. Note that scope for this tests is rootScope so that shouldn't be a problem.


So the error that refers to that line: TypeError: 'undefined' is not a function (evaluating 'spyOn(scope, '$broadcast').andCallThrough()') at .../tests/controllers/sidemenu.js:30


Aucun commentaire:

Enregistrer un commentaire