[英]uibModal provider unknown unitTest
I'm starting to learn how to do unitTesting with jasmine. 我开始学习如何用茉莉花做单元测试。 I read a lot of in internet and SO but I can't solve my problem. 我在互联网和SO上读了很多书,但是我无法解决我的问题。
I have a directive which has a controller. 我有一个带有控制器的指令。 That controller is using the service $uibModal to open a modal when I click an element. 当我单击一个元素时,该控制器正在使用服务$ uibModal打开模式。 I'm trying to inject that service from my test but I can't. 我正在尝试从测试中注入该服务,但我不能。 I read a lot of threads saying that I must pass an instance. 我读了很多线程说必须传递一个实例。 I'm trying to do so but I can't. 我正在尝试这样做,但我不能。 Please any help will be appreciated. 请任何帮助将不胜感激。
.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
}
This is the test where I'm trying to mock this modal. 这是我尝试模拟此模式的测试。
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});
})
I'm getting the error 我遇到了错误
Unknown provider: $uibModalProvider <- $uibModal. 未知提供程序:$ uibModalProvider <-$ uibModal。
Can I inject this service in another way? 我可以通过其他方式注入此服务吗? What I'm doing wrong? 我做错了什么?
PS: I have read this Testing AngularUI Bootstrap modal instance controller PS:我已经阅读了此测试AngularUI Bootstrap模态实例控制器
Angular ui bootstrap $uibModalInstance breaks down unit tests Angular ui bootstrap $ uibModalInstance分解了单元测试
Mocking $modal in AngularJS unit tests 在AngularJS单元测试中模拟$ modal
Try this one: 试试这个:
beforeEach(module(function ($provide) {
$provide.service("$uibModal", function () {
// mock methods here
});
}));
Finally I solved it. 终于我解决了。 It was a silly error. 这是一个愚蠢的错误。 I imported an additional module that I was missing in my tests. 我导入了测试中缺少的其他模块。 After that I could mock my service and use it without any problem like this. 之后,我可以模拟我的服务并使用它,而不会出现任何此类问题。
angular.mock.inject(function($compile, $rootScope, $templateCache, $controller, $uibModal){
scope = $rootScope;
uibModal = $uibModal;
element = angular.element('<directive-tree input-tree=inputTree subsystem=subsystem></directive-tree>');
$compile(element)(scope);
scope.$digest();
controller = $controller('directiveTreeController', {$scope: scope, $uibModal: uibModal});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.