角度Jasmine单元测试:错误:[$injector:unpr]未知提供程序:$confirmProvider
您好,我正在用Jasmine编写我的第一个角度测试,但我不断收到错误,我是测试控制器y确认未运行测试,我是控制器中的注入确认:错误:[$injector:unpr]未知提供程序:$confirmProvider我的代码是: 我的规格角度Jasmine单元测试:错误:[$injector:unpr]未知提供程序:$confirmProvider,jasmine,Jasmine,您好,我正在用Jasmine编写我的第一个角度测试,但我不断收到错误,我是测试控制器y确认未运行测试,我是控制器中的注入确认:错误:[$injector:unpr]未知提供程序:$confirmProvider我的代码是: 我的规格 describe('Controller : diagramaController', function () { var scope, rootScope, controller, $confirm, UserCtrl; beforeEach(f
describe('Controller : diagramaController', function () {
var scope, rootScope, controller, $confirm, UserCtrl;
beforeEach(function () {
module('Mycontrollers');
});
beforeEach(inject(function ($rootScope, $controller, _$confirm_, $routeParams) {
// The injector unwraps the underscores (_) from around the parameter names when matching
scope = $rootScope.$new();
$confirm = _$confirm_;
UserCtrl = $controller('diagramaController', { $scope: scope, $confirm: confirm });
}));
it('Deberia agregar y sumar uno', function () {
//var $scope = {};
//var controller = $controller('actividadController', { $scope: $scope });
var contador = 0;
expect(contadoractual).toBe(contadorGenerado - 1);
});
});
我的控制器:
(function () {
angular.module('Mycontrollers', [])
.controller('diagramaController',['$scope', '$http', '$confirm', '$filter', '$timeout', '$routeParams', '$modal', '$rootScope',
function ($scope, $http, $confirm, $filter, $timeout, $routeParams,$modal, $rootScope)
{
$scope.contador = 1;
})();
我的应用程序在此插入确认器:
(function () {
var app = angular.module('myApp', [
'angucomplete-alt',
'ngDraggable',
'Mycontrollers',
'ngRoute',
'ui.bootstrap',
'Confirmar'
])
})();
};
confirmar js
angular.module("Confirmar", [])
.controller('ConfirmModalController', function ($scope, $modalInstance, data) {
$scope.data = angular.copy(data);
$scope.ok = function () {
$modalInstance.close();
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
})
.value('$confirmModalDefaults', {
template: '<div class="modal-header"><h3 class="modal-title">Confirmar</h3></div><div class="modal-body">{{data.text}}</div><div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">Aceptar</button><button class="btn btn-primary" ng-click="cancel()">Cancelar</button></div>',
controller: 'ConfirmModalController'
})
.factory('$confirm', function ($modal, $confirmModalDefaults) {
return function (data, settings) {
settings = angular.extend($confirmModalDefaults, (settings || {}));
data = data || {};
if ('templateUrl' in settings && 'template' in settings) {
delete settings.template;
}
settings.resolve = { data: function () { return data; } };
return $modal.open(settings).result;
};
})
angular.module(“Confirmar”,[])
.controller('ConfirmModalController',函数($scope,$modalInstance,data){
$scope.data=angular.copy(数据);
$scope.ok=函数(){
$modalInstance.close();
};
$scope.cancel=函数(){
$modalInstance.disclose('cancel');
};
})
.value(“$confirmModalDefaults”{
模板:'Confirmar{{data.text}}AceptarCancelar',
控制器:“ConfirmModalController”
})
.factory(“$confirm”,函数($modal,$confirmModalDefaults){
返回功能(数据、设置){
settings=angular.extend($confirmModalDefaults,(settings |{}));
数据=数据|{};
如果('templateUrl'在设置中&&'template'在设置中){
删除设置.模板;
}
settings.resolve={data:function(){returndata;}};
返回$modal.open(设置).result;
};
})
您需要初始化所有自定义模块以及要测试的模块:
beforeEach(function () {
module('Mycontrollers');
module('Confirmar');
module('myApp');
});
编辑:
是你的plunkr的一个简化版本。你有一些问题。首先,您尝试使用$confirm,它不是标准的angularjs指令,但必须单独添加,这意味着您必须将其注入模块,而不仅仅是控制器
这是如何做到的:
1) 在源代码中添加两个依赖项:
<script src="ui-bootstrap-tpls-0.13.0.min.js"></script>
<script src="angular-confirm.js"></script>
您需要ui引导,因为它是angular confirm指令的依赖项
这些可以下载和下载
2) 注入模块
角度。模块('MyControllers',['angular-confirm'])
.controller('diagramaController',['$scope','$confirm',
功能($scope,$confirm){你也可以在这里发布你在测试中试图模拟的
DiagramController
的代码吗?如果有一个带有代码的plunkr并没有通过测试,那就太好了。嗨,代码在这里:,谢谢’sdon't work,代码在这里:plnkr.co/edit/qqti6bh5hymk7ki5xs0检查我的plunkr示例和解释感谢复制Diana R,我会的eep正在进行angularjs测试。但是,在我的控制器jsplumb中,如果没有实例化,就必须将其放入策略中?控制器{$scope.instance=jsplumb.getInstance({…}从未使用过jsplumb,请检查其用法的演示。