0

I have a service like this.

It is simply just make a http get request.

angular.module('myApp').service('TESTService', ['$http',
    function($http) {
        var request = function(url) {
            return $http({
                method: 'GET',
                url: url
            });
        };
        return {
            get: function(url) {
                return request(url);
            }            
        };
    }
]);

Within my controller, I have called the service

 TESTService.get('/api/product' + id).success(
            function(result) {
                console.log(result)
            }
        );

I need to write the unit test for it

describe('test here', function () {
var testCtrl, scope, httpBackend, testService;

// Initialize the controller and a mock scope
beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_,  _TESTService_) {
    scope = _$rootScope_.$new();
    httpBackend = _$httpBackend_;
    testService = _TESTService_;

    testCtrl = _$controller_('testCtrl', {
        $scope: scope
    });



     it('should return http data', function() {
        var productData = {
           data: [
                {
                  obj: {
                    id:'123'
                  }
                }           
            ]
        }

        httpBackend.expectGET('/api/product/' + id).respond(productData);

        TESTService.get('/api/product/' + id).
            then(function(data) {
                var result = data;
            })

        httpBackend.flush();
        expect(result).toEqual(productData)
    });
 }));

After running the test, I got

Error: Unexpected request: GET /api/product/undefined

How do I write the test to make sure it passes? Any ideas? Thanks a lot!

1
  • Just as a side note, testing a service that is using $http is an integration test, not a unit test. Unit Tests are able to be performed in isolation, without the presence of servers, databases, etc. Normally you don't need to do these kind of integration tests anyway, as $http is already known functional from Angular and your backend service should be tested on the server. In other tests, this service would be mocked. Commented Feb 10, 2015 at 1:00

2 Answers 2

1

Your variable "id" seems to be undefined. If you throw in

var id = 123; 

before this line:

httpBackend.expectGET('/api/product/' + id).respond(productData);

It would call /api/product/123 instead.

So maybe you were looking for this in the first place:

 httpBackend.expectGET('/api/product/' + productData.data[0].obj.id).respond(productData);

 TESTService.get('/api/product/' + productData.data[0].obj.id).

And so on... Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I did what you suggested. It still says Unexpected request: GET /api/product/undefined expect GET /api/product/12345 +1 though
0

Try putting single quotes around the object that's passed into $http, i.e. $http({method: 'GET', 'url', url});

angular.module('myApp').service('TESTService', ['$http',
    function($http) {
        var request = function(url) {
            return $http({
                method: 'GET',
                'url': url
            });
        };
        return {
            get: function(url) {
                return request(url);
            }            
        };
    }
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.