Javascript 如何对';解决';角度UI引导模式组件上的属性

Javascript 如何对';解决';角度UI引导模式组件上的属性,javascript,angularjs,unit-testing,jasmine,angular-ui-bootstrap,Javascript,Angularjs,Unit Testing,Jasmine,Angular Ui Bootstrap,我正在尝试编写一个单元测试,它断言正确的变量正在从Angular ui引导组件发送到ui.bootstrap.modal的resolve属性。以下是我到目前为止的情况: // Controller angular.module('app') .controller('WorkflowListCtrl', function ($scope, $modal) { // Setup the edit callback to open a modal $scope.edit = fu

我正在尝试编写一个单元测试,它断言正确的变量正在从Angular ui引导组件发送到
ui.bootstrap.modal
resolve
属性。以下是我到目前为止的情况:

// Controller
angular.module('app')
  .controller('WorkflowListCtrl', function ($scope, $modal) {
    // Setup the edit callback to open a modal
    $scope.edit = function(name) {
      var modalInstance = $modal.open({
        templateUrl: 'partials/editWorkflowModal.html',
        controller: 'WorkflowEditCtrl',
        scope: $scope,
        resolve: {
          name: function() { return name; }
        }
      });
    };
  });
值得注意的是,
resolve.name
属性必须是Angular UI组件正常工作的函数-之前我尝试过
resolve:{name:name}
,但这不起作用

// Unit Test
describe('Controller: WorkflowListCtrl', function () {

  // load the controller's module
  beforeEach(module('app'));

  var workflowListCtrl,
    scope,
    modal;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {

    scope = $rootScope.$new();
    modal = {
      open: jasmine.createSpy()
    };

    workflowListCtrl = $controller('WorkflowListCtrl', {
      $scope: scope,
      $modal: modal
    });

    it('should allow a workflow to be edited', function() {
      // Edit workflow happens in a modal.
      scope.edit('Barney Rubble');
      expect(modal.open).toHaveBeenCalledWith({
        templateUrl: 'partials/editWorkflowModal.html',
        controller: 'WorkflowEditCtrl',
        scope: scope,
        resolve: {
          name: jasmine.any(Function)
        }
      });
    });
  }));
});
目前,这只是检查
resolve.name
属性是否是一个函数,但我真正想做的是断言
resolve.name
函数返回
Barney-browse
。这种语法显然不起作用:

expect(modal.open).toHaveBeenCalledWith({
  templateUrl: 'partials/editWorkflowModal.html',
  controller: 'WorkflowEditCtrl',
  scope: scope,
  resolve: {
    name: function() { return 'Barney Rubble'; }
  }
});

似乎我不知何故想监视
resolve.name
函数,以检查它是用
Barney Bruse
调用的,但我想不出一种方法来实现这一点。有什么想法吗?

所以我想出了一个办法

$scope
上定义“private”函数:

$scope._resolve = function(item) {
  return function() {
    return item;
  };
};
修改原始的
$scope
函数以调用此“private”方法:

$scope.edit = function(name) {
  var modalInstance = $modal.open({
    templateUrl: 'partials/modal.html',
    controller: 'ModalCtrl',
    scope: $scope,
    resolve: {
      name: $scope._resolve(name)
    }
  });
};
更新测试以模拟此函数并返回原始值,然后可以测试它是否正确传入

it('should allow a workflow to be edited', function() {
  // Mock out the resolve fn and return our item
  spyOn($scope, '_resolve').and.callFake(function(item) {
    return item;
  });

  // Edit workflow happens in a modal.
  scope.edit('Barney Rubble');
  expect(modal.open).toHaveBeenCalledWith({
    templateUrl: 'partials/modal.html',
    controller: 'ModalCtrl',
    scope: scope,
    resolve: {
      name: 'Barney Rubble'
    }
  });
});

也许您可以看到:这可能是另一个解决方案:如何为$scope编写单独的单元测试。\u解析方法?