mardi 28 juillet 2015

Create unit-testing service with promise in Jasmine

I have a service return promise needd unit-test.

var urlQuestions = "user/securityQuestions";
   AccountService.getServerDataForUrl(urlQuestions)
   .then(function (data) {
         $scope.securityQuestions = data;
   }, function (error) {
});

And this is my unit-test:

it('submit should call the AccountService.getServerDataForUrl', function () {
        var urlQuestions = "user/securityQuestions"; 
        var error;
        var deferredSuccess = $q.defer();
        spyOn(AccountService, 'getServerDataForUrl').and.returnValue(deferredSuccess.promise);

        expect(AccountService.getServerDataForUrl).toHaveBeenCalledWith(urlQuestions);

        deferredSuccess.resolve(); 
        $scope.$digest();

        expect(error).toBe(null);
        expect($scope.securityQuestions).toEqual(data);
});

But when I run it, it make some error:
Expected spy getServerDataForUrl to have been called with [ 'user/securityQuestions' ] but it was never called.
Error: Unexpected request: GET user/securityQuestions
No more request expected

Aucun commentaire:

Enregistrer un commentaire