Angular 使用 Karma Jasmine 进行工厂测试。说未知的提供者

Angular factory testing with Karma Jasmine. Says UnKnown provider

这是我的 angular 代码

    var app = angular.module('FleetTask', ['ui.router']);

    app.constant('settings', {
        ApiURL: 'http://localhost/FleetTask.API',
        MsgDuration: 4000,
        ProductURL: '/api/product', 
        ProductGroupURL: '/api/productgroup',
        DataAreaURL: '/api/dataarea'
    });

    app.factory('ProductGroupFactory', ['$http', 'settings', function ($http, settings) {

        return {
            getProductGroupList: function () {
                return $http.get(settings.ApiURL + settings.ProductGroupURL);
            },
            getProductGroupByID: function (id) {
                return $http.get(settings.ApiURL + settings.ProductGroupURL + "/" + id);
            },
            saveProductGroup: function (productGroup) {
                return $http.put(settings.ApiURL + settings.ProductGroupURL, productGroup);
            },
            deleteProductGroup: function (id) {
                return $http.delete(settings.ApiURL + settings.ProductGroupURL + "/" + id);
            },
            getProductList: function () {
                return $http.get(settings.ApiURL + settings.ProductURL);
            },
            getDataAreaList: function (lookup) {
                return $http.get(settings.ApiURL + settings.DataAreaURL + "/dataarealist/" + lookup);
            }
        };

    }]);

    app.controller('ProductGroupController', function ($scope, ProductGroupFactory, $rootScope, settings) {

        function init(pageIndex) {
            $rootScope.pageTitle = "Product Group";

            // Complete functionality       

        };

        init();
    });

这是我的测试规格:

    describe('FleetTask', function () {
        beforeEach(function () {
            module('FleetTask');
            module('ui.router');
        });

        describe('ProductGroupFactory', function () {

            var scope, controller, productGroupFactory, settings;

            beforeEach(inject(function ($injector, $rootScope, $controller, $provide) {

                scope = $rootScope.$new();
                settings = $injector.get('settings');

                productGroupFactory = {
                    save: jasmine.createSpy()
                };

                $provide.value('ProductGroupFactory', productGroupFactory);

            }));


            it('Access Factory', function () {
                expect(settings).toBeDefined();
            });
        });
    });

这是我遇到的错误

错误提示未知提供商。看起来我没有按照正确的方式注入工厂。 'settings' 服务注入正确,但工厂注入失败。帮助我找出注入工厂的正确流程。谢谢

答案:我根据答案得出的测试规范。

     describe('FleetTask', function () {

        var scope, controller, pgFactory, constants, httpBackend;

        beforeEach(function () {
            module('FleetTask');
            module('ui.router');
            module('angulartics');
            module('angulartics.google.analytics');

            inject(function (_settings_, $injector, $controller, _$rootScope_) {

                $rootScope = _$rootScope_;
                scope = $rootScope.$new();
                constants = _settings_;

                pgFactory = function () {
                    return $injector.get('ProductGroupFactory');
                };

                httpBackend = $injector.get('$httpBackend');
                httpBackend.whenGET(constants.ApiURL + constants.ProductURL).respond(function (data) { scope.products = data; });
                httpBackend.expectGET(constants.ApiURL + constants.ProductGroupURL);
                httpBackend.expectGET(constants.ApiURL + constants.DataAreaURL + "/dataarealist/0");

                controller = $controller;
                controller('ProductGroupController', { '$scope': scope });
            });

        });

        it('Check if settings service is defined', function () {
            expect(constants).toBeDefined();
        });

        it('Check if ProductGroupFactory is defined', function () {
            expect(pgFactory).toBeDefined();
        });

        it('Check if controller is defined', function () {       
            expect(controller).toBeDefined();
        });

        it('Access Controller', function () {
            expect(scope.error).toBe('');
        });

        it('Check if controller is defined', function () {
            var prds = scope.products;
            var len = prds.length;

            expect(len).toBe(0);
        });

    });

您需要访问 $provide 作为模块规范的一部分,它不能被注入。即

beforeEach(module("FleetTask", function ($provide) {
     productGroupFactory = jasmine.createSpyObj('productGroupFactory', ['save']);
      $provide.value('ProductGroupFactory', productGroupFactory);
}));



beforeEach(inject(function (_settings_, $rootScope, $controller) {
     scope = $rootScope.$new();
     settings = _settings_;
     //...
 }));


 it('Access Factory', function () {
     expect(settings).toBeDefined();
  });