mardi 27 janvier 2015

Reset broadcast() after each test case while using andCallThrough()

I am using the code bellow to reset $broadcast after each test case, but it seems it $rootScope.$broadcast.reset(); doesn't function properly, since the test bellow should return 1, but it returns 6.


Seems that the reason for this is andCallThrough(), since before I used it without andCallThrough() function, but after some refactor it gave me an error that TypeError: Cannot read property 'defaultPrevented' of undefined, so I had to use to prevent that error.


The question is how can i reset the broadcast while using andCallThrough or is there another more precise approach?



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: 'user@email.com', 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);
});

Aucun commentaire:

Enregistrer un commentaire