Cómo espiar la función anónima usando Jasmine
Estoy usando Jasmine para probar mi aplicación angular y quiero espiar una función anónima. Uso del servicio de notificación angularhttps://github.com/cgross/angular-notify, Quiero saber si se ha llamado o no a la función de notificación.
Aquí está mi 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;
}
}
});
Y aquí está mi prueba:
'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();
});
});
Un obvio regreso:
Error: No method name supplied on 'spyOn(notify)'
Porque debería ser algo así como spyOn (notificar, 'método'), pero como es una función anónima, no tiene ningún método.
Gracias por tu ayuda.