【发布时间】:2014-03-07 09:55:17
【问题描述】:
编辑:本文结尾处的快速和肮脏的解决方案
我正在使用来自 AngularUI-Bootstrap 的模态窗口,其方式与网站上的解释相同,只是我拆分了文件。 因此我有:
CallingController.js:
$scope.delete = function () {
if ($scope.selected.length > 0) {
// [...]
// preparing data
// [...]
var modalInstance = $modal.open({
templateUrl: 'views/modalView.html',
controller: 'modalCtrl',
resolve: {
itemArray: function () {
return $scope.selected;
}
}
});
modalInstance.result.then(function (confirm) {
if (confirm === true) {
// [...]
// treat
// [...]
}
});
}
};
modalController.js:
myAppControllers.controller('modalCtrl',
function ($scope, $modalInstance, itemArray) {
$scope.accept = function () {
$modalInstance.close(true);
};
$scope.reject = function () {
$modalInstance.close(false);
};
$scope.itemArray = itemArray;
});
当我使用 Karma 测试此代码时(在 karma 配置文件中加载了 ui-bootstrap-tpls.min.js 文件),我收到以下错误: 错误:[$injector:unpr] [http://errors.angularjs.org/1.2.15-build.2389+sha.c5f2f58/$injector/unpr?p0=%24modalInstanceProvider%20%3C-%20%24modalInstance]1 at Error (native),意思是茉莉无法找到 $modalInstance 的提供者。
我什至没有在这个控制器上测试东西,还没有,但这是我的 jasmine 测试文件:
testModalController.js:
describe('Controller: modalCtrl', function () {
beforeEach(module('myApp'));
var Ctrl;
var scope;
// Initialize the controller and a mock scope
beforeEach(inject(
function ($controller, $rootScope) {
scope = $rootScope.$new();
Ctrl = $controller('modalCtrl', { $scope: scope });
})
);
describe('Initial state', function () {
it('should instantiate the controller properly', function () {
expect(Ctrl).not.toBeUndefined();
});
it('should initialize its values properly', function () {
});
});
});
你对这个问题有任何线索吗?这不是我使用(和测试)的第一个“外部”模块,我做了与其他模块相同的事情,只是这次它不起作用,我不知道为什么。
===========================================强>
编辑:快速且可能很脏的解决方案:
好的,所以基于 Jasmine 控制器实例化中的作用域模拟方法,我想出了如何“解决”我的问题,但它可能很脏,所以如果你找到更好的方法,请随时发表评论我的意图。
testModalController.js:
describe('Controller: modalCtrl', function () {
beforeEach(module('myApp'));
var Ctrl;
var scope;
var modalInstance;
// Initialize the controller and a mock scope
beforeEach(inject(
function ($controller, $rootScope, _$modal_) {
scope = $rootScope.$new();
modalInstance = _$modal_.open({
templateUrl: 'views/modalView.html'
});
Ctrl = $controller('modalCtrl', {
$scope: scope,
$modalInstance: modalInstance,
itemArray: function () { return ['a', 'b', 'c']; }
});
})
);
describe('Initial state', function () {
it('should instantiate the controller properly', function () {
expect(Ctrl).not.toBeUndefined();
});
it('should initialize its values properly', function () {
});
});
});
这样,Jasmine 不再搜索提供程序,因为您已经注入了应该需要这些提供程序的项目。它有效,但我相信它可以以更好的方式完成......
【问题讨论】:
-
你需要加载引导模块('ui.bootstrap')或者可能('ui.bootstrap.modal')。试着告诉我。还有一个笨拙的人总是有帮助。
-
同样的事情。添加了依赖项,但在控制台窗口中不断弹出相同的错误。有什么解决办法吗?是什么触发了它?我试过here,它工作正常,但在我的应用程序向左一步,它在控制台中发送这个地狱般的错误。
标签: angularjs jasmine karma-runner angular-ui-bootstrap karma-jasmine