I want to test what happens if Meteor.logout function has error, or not. How I can test callback from Meteor.logout? How I can force pass error to callback function for tests?
I create $auth service. It is look like:
angular.module('myModule')
.service('$auth', Auth);
function Auth($q) {
this.logout = () => {
let deferred = $q.defer();
Meteor.logout((err) => {
if(err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
return deferred.promise;
};
}
Part of my tests:
describe('logout()',function() {
var deferred;
var error = {
reason: 'test reason'
};
beforeEach(function() {
deferred = $q.defer();
spyOn($auth, 'logout').and.returnValue(deferred.promise);
});
it('should be rejected with error', function(done) {
deferred.reject(error);
$auth.logout().catch(function(res) {
expect(res).toEqual(error);
done();
});
$rootScope.$apply();
});
it('should be resolved', function(done) {
deferred.resolve();
$auth.logout().then(function(res) {
expect(res).toBeUndefined(res);
done();
});
$rootScope.$apply();
});
});
Aucun commentaire:
Enregistrer un commentaire