如何测试依赖于先前测试通过的AngularJS服务?

时间:2017-09-05 09:20:05

标签: angularjs karma-jasmine

我有一个AngularJS 1.5.9服务的通过测试,我想在一个单独的it()中测试同一服务上的另一个函数调用。问题是第二个服务功能的结果取决于第一个服务调用的传递结果;当为每个it()重新实例化服务时,第一次测试的成功结果实际上就会丢失。

我的问题基于this question's回答。

我认为我想做的是这样的,第一次测试中测试的服务功能通常用于第二次测试:

describe('user-service', function () {
    var $httpBackend, $q, $rootScope;
    var mockUserData = { "d": { "firstName": "Matt", "lastName": "Lenny" };
    var mockCatalogueData = { "d": { "title": "A Feast of Crows" };

    beforeEach(module('users'));

    beforeEach(inject(function (_$httpBackend_,_$q_,_$rootScope_) {
        $httpBackend = _$httpBackend_;
        $q = _$q_;
        $rootScope = _$rootScope_;

        $httpBackend.when('GET', /(.*)\/user\/api/).respond(200, mockUserData);
        $httpBackend.when('GET', /(.*)\/cat\/api/).respond(200, mockCatalogueData);
    }));

    it('should return the user object', inject(function (userService) {
        var user;

        var deferred = $q.defer();
        var promise = deferred.promise;

        promise.then(function (response) {
            user = response;
        });

        userService.getUserInfo().then(function (response) {
            deferred.resolve(response);
        });

        $rootScope.$digest();

        $httpBackend.flush();

        expect(user).toEqual(mockUserData.d);
    }));

    it('should return the catalogue object', inject(function (userService) {
        var catalogue;

        var deferred = $q.defer();
        var promise = deferred.promise;

        promise.then(function (response) {
            catalogue = response;
        });

        userService.getUserInfo(); // populates internal userService data

        userService.getCatalogueInfo().then(function (response) {
            deferred.resolve(response);
        });

        $rootScope.$digest();

        $httpBackend.flush();

        expect(user).toEqual(mockCatalogueData.d);
    }));
});

1 个答案:

答案 0 :(得分:0)

我发现这有效,但我不完全确定这是否是正确和正确的方法来执行测试:

describe('user-service', function () {
    var $httpBackend, $q, $rootScope;
    var mockUserData = { "d": { "firstName": "Matt", "lastName": "Lenny" };
    var mockCatalogueData = { "d": { "title": "A Feast of Crows" };

    beforeEach(module('users'));

    beforeEach(inject(function (_$httpBackend_,_$q_,_$rootScope_) {
        $httpBackend = _$httpBackend_;
        $q = _$q_;
        $rootScope = _$rootScope_;

        $httpBackend.when('GET', /(.*)\/user\/api/).respond(200, mockUserData);
        $httpBackend.when('GET', /(.*)\/cat\/api/).respond(200, mockCatalogueData);
    }));

    it('should return the user object', inject(function (userService) {
        var user;

        var deferred = $q.defer();
        var promise = deferred.promise;

        promise.then(function (response) {
            user = response;
        });

        userService.getUserInfo().then(function (response) {
            deferred.resolve(response);
        });

        $rootScope.$digest();

        $httpBackend.flush();

        expect(user).toEqual(mockUserData.d);
    }));

    it('should return the catalogue object', inject(function (userService) {
        var catalogue;

        var deferred = $q.defer();
        var promise = deferred.promise;

        userService.getUserInfo().then(function() { // populates internal userService data
            promise.then(function (response) {
                catalogue = response;
            });

            userService.getCatalogueInfo().then(function (response) {
                deferred.resolve(response);
            });
        });


        $rootScope.$digest();

        $httpBackend.flush();

        expect(user).toEqual(mockCatalogueData.d);
    }));
});