gpt4 book ai didi

angularjs - 尝试测试工厂时遇到注入(inject)问题

转载 作者:行者123 更新时间:2023-11-28 20:14:05 29 4
gpt4 key购买 nike

总的来说,我对 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();
});
});

编辑

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

表格

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-router 包含在您的 karma.conf.js 文件中。

关于angularjs - 尝试测试工厂时遇到注入(inject)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44011957/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com