【发布时间】:2016-07-14 13:37:31
【问题描述】:
我开始学习如何使用 jasmine 进行单元测试。我在互联网上阅读了很多内容,但我无法解决我的问题。
我有一个带有控制器的指令。当我单击一个元素时,该控制器正在使用服务 $uibModal 打开一个模式。我正在尝试从我的测试中注入该服务,但我不能。我读了很多线程说我必须传递一个实例。我正在尝试这样做,但我做不到。请任何帮助将不胜感激。
.controller('myController', ['$scope', '$uibModal', function($scope, $uibModal){
var self = this;
//OTHER CODE
self.openMyModal = function(dataInput) {
var modalInstance = $uibModal.open({
animation: true,
bindToController: true,
templateUrl: 'app/myComponent/modals/component-modal.html',
controllerAs: 'componentModalCtrl',
controller: 'componentModalController',
windowClass: 'semi-modal semi-modal--large',
scope: $scope
})
}
//OTHER CODE
}
这是我试图模拟此模式的测试。
beforeEach(function(){
angular.mock.module('templates');
angular.mock.module('app.components.myComponent');
angular.mock.inject(function($compile, $rootScope, $templateCache, $controller){
scope = $rootScope;
modalInstance = { close: function(){}, dismiss: function(){}, open: function(){}
};
//Initializing element and doing compile and digest
controller = $controller('myController', {$scope: scope, $uibModal: modalInstance});
})
我收到了错误
未知提供者:$uibModalProvider
我可以通过其他方式注入此服务吗?我做错了什么?
P.S:我已经读过这个Testing AngularUI Bootstrap modal instance controller
Angular ui bootstrap $uibModalInstance breaks down unit tests
【问题讨论】:
标签: javascript angularjs twitter-bootstrap unit-testing jasmine