Como espionar uma função anônima usando Jasmine
Estou usando o Jasmine para testar meu aplicativo angular e quero espionar uma função anônima. Usando o serviço de notificação angularhttps://github.com/cgross/angular-notify, Quero saber se a função de notificação foi chamada ou não.
Aqui está o meu controlador:
angular.module('module').controller('MyCtrl', function($scope, MyService, notify) {
$scope.isValid = function(obj) {
if (!MyService.isNameValid(obj.name)) {
notify({ message:'Name not valid', classes: ['alert'] });
return false;
}
}
});
E aqui está o meu teste:
'use strict';
describe('Test MyCtrl', function () {
var scope, $location, createController, controller, notify;
beforeEach(module('module'));
beforeEach(inject(function ($rootScope, $controller, _$location_, _notify_) {
$location = _$location_;
scope = $rootScope.$new();
notify = _notify_;
notify = jasmine.createSpy('spy').andReturn('test');
createController = function() {
return $controller('MyCtrl', {
'$scope': scope
});
};
}));
it('should call notify', function() {
spyOn(notify);
controller = createController();
scope.isValid('name');
expect(notify).toHaveBeenCalled();
});
});
Um retorno obviamente:
Error: No method name supplied on 'spyOn(notify)'
Como deve ser algo como spyOn (notify, 'method'), mas como é uma função anônima, ele não possui nenhum método.
Obrigado pela ajuda.