如何测试在angularjs / karma中使用$ injector的工厂?

时间:2015-11-27 15:02:16

标签: angularjs unit-testing karma-runner publish-subscribe

我目前正在尝试为我的angularjs应用测试一个PubSub服务,这将是多个服务之间的缓冲区。

例如: 我的工厂A调用$ injector注入我的Pubsub服务进行订阅/发布,而没有循环依赖声明。 PubSub呼叫服务B.

这个例子很好用。但我在单元测试中苦苦挣扎。 正因为如此:" 错误:注射器已经创建,无法注册模块!" 我不知道如何成功完成测试。任何帮助都很棒,thx。

添加代码:

.factory('LocationManager', ['$rootScope', '$state', '$localStorage', '$injector', 'Location', 'Notification',
    function($rootScope, $state, $localStorage, $injector, Location, Notification) {
        var location_mgr = {
            Location: Location,
            saveLocation: saveLocation
        };
        return location_mgr;

        function saveLocation(location) {
            var PubSubManager = $injector.get(['PubSubManager']);
                PubSubManager.subscribe('loadUser', $localStorage.user_id);
                PubSubManager.subscribe('loadProfile', [location.id, 'location']);
            var params = location.id ? {id: location.id} : {};
            var action = location.id ? Location.update : Location.save;
            return action(params, location,
                function (response) {
                    PubSubManager.publish('loadUser', $localStorage.user_id);
                    PubSubManager.publish('loadProfile', [location.id, 'location']);
                    $state.go('ls.location.id.view', {id: response.id});
                },
                function (result) {
                    console.log(result);
                });
        }

.factory('PubSubManager', ['$injector',
    function ($injector) {
        var returnedResult,
            pubsub_mgr = {
                publish: publish,
                subscribe: subscribe,
                unsubscribe: unsubscribe
            };
        return pubsub_mgr;

        function publish(id, params) {
            PubSub.publishSync( id, params );
            return returnedResult;
        }

        function subscribe(id) {
            PubSub.subscribe( id, switchFunction );
        }

        function unsubscribe(id) {
            PubSub( id ).unsubscribe( id );
        }

        function switchFunction(id, params) {
            switch (id) {
                case 'getLocations':
                    $injector.invoke(function (LocationManager) {
                        returnedResult = LocationManager.getLocations(params);
                    });
                    break;
                default:
                    break;
            }
        }
    }]);

单元测试:

describe("LocationManager factory:", function () {
var httpBackend, rootScope, state, localStorage;
var LocationManager, Location;

beforeEach(module('ionic', 'ngResource', 'ui-notification', 'ngStorage'));
beforeEach(module('location_services'));

beforeEach(inject(function ($httpBackend, _$rootScope_, _$state_, _$localStorage_, _LocationManager_, _Location_) {
    httpBackend = $httpBackend;
    rootScope = _$rootScope_;
    state = _$state_;
    localStorage = _$localStorage_
    LocationManager = _LocationManager_;
    Location = _Location_;
}));

describe('saveLocation function,', function () {
    it('should create a location', function () {
        beforeEach(module('pubSub_services'));
        beforeEach(function () {
            spyOn(state, 'go');
        });
        httpBackend.expect('POST', '/api/location').respond({status: 200, message: ''});
        var location = {
            name: 'new_location'
        };
        LocationManager.saveLocation(location).$promise.then(
            function (result) {
                data = result;
                expect(state.go).toHaveBeenCalled();
            }
        );
        httpBackend.flush();
        expect(data.status).toBe(200);
    });

0 个答案:

没有答案