angularjsresttestingjasminehttpbackend

An issue with testing $resource service by $httpBackend


I guess I miss something. Have spent some time trying to understand why my test is not working.

The code.

angular.module('services')
    .factory('UserPreferencesService', ['$resource', function ($resource)
    {
        return $resource('/rest/user-preferences/:id', {},
        { getById: { method: "GET", params: { id:'@id'}} });

    }
]);

The test:

it('should get by Id', function() {
    //given
    var preferences = {language: "en"};

    httpBackend.whenGET('/rest/user-preferences/1').respond(preferences);

    //when
    service.getById( {id:1} ).$promise.then(function(result) {

        console.log("successssssssssssssssssssssssssssssssssssss");

     //then
     expect(result).toEqual(preferences);
    }, function() {
        console.log("something wrong");
    })
});

It never triggers: "successssssssssssssssssssssssssssssssssssss".

What did I miss?


Solution

  • There were some things wrong and other things missing in your code.

    The main problem was that you were not calling the flush() function that emulates the response from the server, so the $promise was never resolved. Also, bear in mind that when the promise gets resolved, the response that you get it's a promise, meaning that this: expect(result).toEqual(preferences); won't work, but this: expect(result.lang).toEqual(preferences.lang); will.

    Here you have a fixed version of your code:

    The service:

    angular.module('services',['ngResource'])
        .factory('UserPreferencesService', ['$resource', function ($resource)
        {
          return $resource('/rest/user-preferences/:id', {},
            { getById: { method: "GET", params: { id:'@id'}} });
        }
    ]);
    

    The Test:

    describe('My Suite', function () {
      var httpBackend, service; 
      beforeEach(module('services'));
    
      beforeEach(inject(function (_$httpBackend_, UserPreferencesService) {
          httpBackend = _$httpBackend_;
          service = UserPreferencesService;
      }));
    
      describe('My Test', function () {
          it('should get by Id', function() {
            var preferences = {language: "en"};
            var result = {};
    
            httpBackend.whenGET('/rest/user-preferences/1').respond(preferences);
    
            service.getById({id:1}).$promise.then(function(result_) {
              result = result_;
            });
    
            expect(result.language).not.toEqual(preferences.language);
    
            httpBackend.flush();
    
            expect(result.language).toEqual(preferences.language);
        });
      });
    });
    

    Working Example