Angularjs 角度ui引导$uibModalInstance分解单元测试

Angularjs 角度ui引导$uibModalInstance分解单元测试,angularjs,twitter-bootstrap,angular-ui-bootstrap,bootstrap-modal,Angularjs,Twitter Bootstrap,Angular Ui Bootstrap,Bootstrap Modal,通过使用$uibModal服务打开模式窗口,我们需要在模式控制器中注入$uibModalInstance,以关闭或关闭模式窗口,这种注入破坏了我的单元测试 script.js angular.module('demo', ['ui.bootstrap']) .controller('MainController', MainCtrl) .controller('UIModalController', UIModalCtrl); /** @ngInject */ function Mai

通过使用
$uibModal
服务打开模式窗口,我们需要在模式控制器中注入
$uibModalInstance
,以关闭或关闭模式窗口,这种注入破坏了我的单元测试

script.js

angular.module('demo', ['ui.bootstrap'])
  .controller('MainController', MainCtrl)
  .controller('UIModalController', UIModalCtrl);

/** @ngInject */
function MainCtrl($log, $uibModal) {
  var vm = this;
  vm.openModal = function() {
    $log.log('Opening Modal..');
    $uibModal.open({
      templateUrl: 'modal.html',
      controller: 'UIModalController',
      controllerAs: 'modal'
    });
 };
}

/** @ngInject */
function UIModalCtrl($log, $uibModalInstance) {
 var vm = this;
  vm.ok = function() {
    $log.log('Ok clicked');
    $uibModalInstance.close();
  };
}
describe('MainContrller', function() {
  var vm;

  beforeEach(module('demo'));

  beforeEach(inject(function(_$controller_) {
    vm = _$controller_('UIModalController', {});
  }));

  it('should define a ok function', function() {
    expect(vm.ok).toBeDefined();
    expect(angular.isFunction(vm.ok)).toBeTruthy();
  });

  it('should close the modal window if Ok button is pressed', inject(function($uibModalInstance) {
    spyOn($uibModalInstance, 'close').and.callThrough();
    vm.ok();
    expect($uibModalInstance.close).toHaveBeenCalled();
  }));
});
modal.html

<div class='modal-header'>
  <h3>Hi there!</h3>
</div>

<div class='modal-body'>
  Some content goes here
</div>

<div class='modal-footer'>
  <button class='btn btn-primary' ng-click='modal.ok()'>Ok</button>
</div>
下面是一个例子来说明这个问题:

注意:我面临的问题不是关闭模式(因为它工作),问题是我的单元测试在注入
$uibModalInstance
之前工作

引发的错误是:

Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance

错误:[$injector:unpr]未知提供程序:$uibModalInstanceProvider查看我的答案。他们将名称从
$modalInstance
更改为
$uibModalInstance
,但答案仍然正确。很好的解释,解决了我的问题,非常感谢@SunilD。