尝试测试工厂时遇到注入问题

Trying to test a factory and I am having injection issues

我对 angular 和一般测试比较陌生。我正在尝试设置一个基本测试,在我开始尝试测试其他任何东西之前先查看对象是否已定义。

我收到这个错误:

Error: [$injector:unpr] Unknown provider: $stateParamsProvider <- $stateParams <- Form

然而,当我尝试对其他测试文件进行此基本测试时,此错误并未出现。

工厂

angular.module('omnibyte_inspect_web.objects')
.factory('CommonQuestions', ['common_questions_factory', 'Form', '$rootScope',
    function (common_questions_factory, Form, $rootScope) {
    // Ctor
    function CommonQuestions(data) {
        var keys = Object.keys(data);
        for (var i = 0; i < keys.length; i++) {
            this[keys[i]] = data[keys[i]];
        }
    };

    CommonQuestions.prototype.Select = function () {
        this.Id = guid();

        Form.CurrentForm().AddCommonQuestion(angular.copy(this));
    };
    CommonQuestions.prototype.Remove = function () {
        common_questions_factory.delete(this.Id).then(function () {
            window.location.reload();
        });
    };

        // Static Methods
    CommonQuestions.Current = function () {
        return $rootScope.config_info;
    };
    CommonQuestions.GetAll = function (callback) {
        common_questions_factory.get().then(function (data) {
            var collection = [];

            for (var i = 0; i < data.length; i++) {
                collection.push(new CommonQuestions(data[i]));
            }

            callback(collection);
        });
    };

    return CommonQuestions;
}]);

测试文件

    describe('CommonQuestions Test', function () {
    beforeEach(module('omnibyte_inspect_web.objects'));

    var common_questions_factory, $rootScope, CommonQuestions, Form;

    beforeEach(inject(function (_common_questions_factory_, _Form_, _$rootScope_, _CommonQuestions_) {
        common_questions_factory = _common_questions_factory_;
        Form = _Form_;
        $rootScope = _$rootScope_;
        CommonQuestions = _CommonQuestions_;

        spyOn(CommonQuestions, 'GetAll');
        spyOn(common_questions_factory, 'get');
        spyOn(CommonQuestions, 'Current');       
    }));

    it('should have CommonQuestions be defined', function () {
        expect(CommonQuestions).toBeDefined();
    });
});

编辑

在多个文件上有同样的问题,但它似乎来自我的表单文件。即:

表格

angular.module('omnibyte_inspect_web.objects')
.factory('Form', ['forms_factory', 'authentication_service', 'common_questions_factory', 'formdata_factory', 'missinginformation_factory', '$stateParams', 'Question', 'LocationContact', '$rootScope', '$ionicPopup', '$state',
function (forms_factory, authentication_service, common_questions_factory, formdata_factory, missinginformation_factory, $stateParams, Question, LocationContact, $rootScope, $ionicPopup, $state) {

第二次编辑

将此模块 beforeEach(module('ui.router')); 放入我的测试文件后,我得到:

Error: [$injector:unpr] Unknown provider: $ionicPopupProvider <- $ionicPopup <- Form

将此模块 beforeEach(module('$ionic')); 放入我的测试文件后,错误消失了;但是,我得到 Expected undefined to be defined。这个测试在我的所有其他文件中都有效。

$stateParams 是 angular-ui/ui-router 中的一项服务。确保 ui-路由器包含在您的 karma.conf.js 文件中。

找到解决方案。我需要将这些模块添加到测试文件中:

beforeEach(module('omnibyte_inspect_web.objects'));
beforeEach(module('ui.router'));
beforeEach(module('ionic'));

并且在我的 karma.conf.js 文件中被注释掉了:

'www/lib/ionic/js/ionic.bundle.js',

进行这些更改后,它已修复。