mercredi 29 avril 2015

Angular testing http request error

I am trying to test a http request with dynamic url

I have something in my service file like

My service file.

//other service codes..
//other service codes..

return $http.get('/api/product/' + id + '/description')
//id is dynamic 

Test file

describe('test', function () {
    beforeEach(module('myApp'));

    var $httpBackend, testCtrl, scope;

    beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_) {
        scope = _$rootScope_.$new();
        $httpBackend = _$httpBackend_;
        testCtrl = _$controller_('testCtrl', {
            $scope: scope
        });
    }));

    it('should check the request', function() {
        $httpBackend.expectGET('/api/product/12345/description').respond({name:'test'});
        $httpBackend.flush();
        expect(scope.product).toBeDefined();
    })
});

I am getting an error saying

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

I am not sure how to test the dynamic url. Can anyone help me about it? Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire