Sto provando a scrivere un test unitario che verifica che venga chiamato $rootScope.$broadcast('myApiPlay', { action : 'play' });
.
Ecco l'myapi.js
angular.module('myApp').factory('MyApi', function ($rootScope) {
var api = {};
api.play = function() {
$rootScope.$broadcast('myApiPlay', { action : 'play' });
}
return api;
});
E qui è la mia prova Unità:
describe('Service: MyApi', function() {
// load the service's module
beforeEach(module('myApp'));
// instantiate service
var MyApi;
var rootScope;
beforeEach(function() {
inject(function ($rootScope, _MyApi_) {
MyApi = _MyApi_;
rootScope = $rootScope.$new();
})
});
it('should broadcast to play', function() {
spyOn(rootScope, '$broadcast').andCallThrough();
rootScope.$on('myApiPlay', function (event, data) {
expect(data.action).toBe('play');
});
MyApi.play();
expect(rootScope.$broadcast).toHaveBeenCalledWith('myApiPlay');
});
});
Qui è l'errore che sto ottenendo durante l'esecuzione grunt test
:
PhantomJS 1.9.7 (Windows 7) Service: MyApi should broadcast to pause FAILED
Expected spy $broadcast to have been called with [ 'myApiPlay' ] but it was never called.
Ho anche provato con expect(rootScope.$broadcast).toHaveBeenCalled()
e sto avendo un errore simile: Expected spy $broadcast to have been called.
.
Vorrei verificare che tale metodo sia stato effettivamente chiamato con i parametri corretti.
Grazie!
Avete solo bisogno di cambiare l'ordine? Potresti dover aspettare prima che venga effettivamente chiamato, nel tuo caso lo chiami, quindi aspettati. Puoi anche confermare il conteggio delle chiamate. – hassassin
Ho cambiato l'ordine, lo stesso errore. – Andrea