Sto usando il codice qui sotto per reimpostare $broadcast
dopo ogni caso di test, ma sembra che $rootScope.$broadcast.reset();
non funzioni correttamente, dal momento che il test qui sotto dovrebbe restituire 1, ma restituisce 6.Ripristina broadcast() dopo ogni caso di test durante l'utilizzo di eCallThrough()
sembra che la ragione di questo è andCallThrough()
, dal momento che prima ho usato senza andCallThrough()
funzione, ma dopo un po 'refactoring mi ha dato un errore che TypeError: Cannot read property 'defaultPrevented' of undefined
, così ho dovuto usare per evitare tale errore.
La domanda è: come posso ripristinare lo broadcast
durante l'utilizzo di andCallThrough
oppure esiste un altro approccio più preciso?
beforeEach(function() {
spyOn($http, 'post');
spyOn($rootScope, '$broadcast').andCallThrough();
});
afterEach(function() {
$http.post.reset();
$rootScope.$broadcast.reset();
});
it('should make a POST request to API endpoint', function() {
$http.post.andCallThrough();
var response = { id: '123', role: 'employee', email: '[email protected]', username: 'someUsername' };
$httpBackend.expectPOST(apiUrl + 'login').respond(response);
service.login();
$httpBackend.flush();
$timeout.flush();
expect($rootScope.$broadcast.callCount).toBe(1);
expect($rootScope.$broadcast).toHaveBeenCalledWith(AUTH_EVENTS.loginSuccess, response);
});
Se la chiamata a "aspettarsi" di testare "callCount" viene spostata sulla prima riga del test, cosa significa "callCount" uguale poi? Mi chiedo se qualcos'altro nel test sta chiamando '$ broadcast'. –