samedi 31 janvier 2015

unit test for service with a promise

I have a service that i wrote for a project i am currently working on and i am trying to write a unit test for it. Below is the code for the service



angular.module('services').factory('Authorization', ['$q', 'Refs', function($q, Refs) {

function isAuthorized() {
var deferred = $q.defer();

var authData = Refs.rootRef.getAuth();
var adminRef;

if(authData.google) {
adminRef = Refs.rootRef.child('admins').child(authData.uid);
} else {
adminRef = Refs.rootRef.child('admins').child(authData.auth.uid);
}

adminRef.on('value', function(adminSnap) {
deferred.resolve(adminSnap.val());
});
return deferred.promise;
}

return {
isAuthorized: isAuthorized
};
}]);


I have written a unit test for it but anytime i run the test i get this error message ' Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.'


Below is the code for the unit test i wrote for the service:



'use strict';

describe('Authorization Service', function() {

var Refs, $q, Authorization, authData, scope, deferred;

beforeEach(angular.mock.module('Sidetime'));

beforeEach(inject(function(_Refs_, $rootScope, _$q_, _Authorization_) {
Refs = _Refs_;
$q = _$q_;
Authorization = _Authorization_;
}));

iit('return admin object', function(done) {
var result;

Authorization.isAuthorized = function() {
deferred = $q.defer();
authData = {google: 'uid:112222'};

if(authData.google) {
deferred.resolve(authData);
}

return deferred.promise;
};

Authorization.isAuthorized().then(function(result) {
console.log('result', result);
expect(result).toEqual(authData);
//done();
});
});
});


I am not sure I am writing the unit test properly. I will appreciate if someone could show be a better way of writing the unit test for this service. Thanks for your anticipated assistance.


Aucun commentaire:

Enregistrer un commentaire