mercredi 29 avril 2015

how to test the service in the app

I am trying to test the factory for Angular..

my Service file

angular.module('myApp').factory('testService', ['$resource', function ($resource) {
    return $resource(
        '/api/product:id',
        { id: '@id'},
        {
            'query': {
                url: '/api/v0.1/item/:itemId',
                method: 'GET'
            }            
        }
    );
}]);

my test file

describe('Factory test', function () {
    beforeEach(module('myApp'));
    var $httpBackend, $rootScope, mockData;

    beforeEach(inject(function (_$rootScope_, _$httpBackend_, _testService_) {
        $rootScope = _$rootScope_;
        $httpBackend = _$httpBackend_;
        mockData = _testService_;   
    }));

    it('should return data', function() {
        $httpBackend.expect('GET','/api/product/').respond(200,'success');
        var result = mockData.$query(); .//getting error here. 'undefined is not a function'
        $httpBackend.flush();
        expect(result).toBeDefined();
    })
})

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

Aucun commentaire:

Enregistrer un commentaire