如何在Angular-UI Bootstrap Modal组件上单元测试'resolve'属性

时间:2021-09-15 19:39:48

I'm trying to write a unit test that asserts the correct variable is being sent to the resolve property of ui.bootstrap.modal from the Angular-UI Bootstrap components. Here is what I have so far:

我正在尝试编写一个单元测试,断言正确的变量从Angular-UI Bootstrap组件发送到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; }
        }
      });
    };
  });

It's worth noting that the resolve.name property must be a function for the Angular-UI component to work correctly - previously I had tried resolve: { name: name } but this didn't work.

值得注意的是,resolve.name属性必须是Angular-UI组件正常工作的函数 - 之前我曾尝试解决:{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)
        }
      });
    });
  }));
});

At the moment, this is just checking that the resolve.name property is a function, but what I'd really like to do is assert the resolve.name function returns Barney Rubble. This syntax obviously doesn't work:

目前,这只是检查resolve.name属性是一个函数,但我真正想做的是断言resolve.name函数返回Barney Rubble。这种语法显然不起作用:

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

It seems like I somehow want to spy on the resolve.name function to check it was called with Barney Rubble but I can't figure out a way to do that. Any ideas?

似乎我想以某种方式窥探resolve.name函数以检查它是否与Barney Rubble一起调用,但我无法找到一种方法来做到这一点。有任何想法吗?

1 个解决方案

#1


5  

So I have figured out a way to do this.

所以我找到了一种方法来做到这一点。

Define a 'private' function on $scope:

在$ scope上定义'private'函数:

$scope._resolve = function(item) {
  return function() {
    return item;
  };
};

Modify the original $scope function to call this 'private' method:

修改原始$ scope函数以调用此'private'方法:

$scope.edit = function(name) {
  var modalInstance = $modal.open({
    templateUrl: 'partials/modal.html',
    controller: 'ModalCtrl',
    scope: $scope,
    resolve: {
      name: $scope._resolve(name)
    }
  });
};

Update your tests to mock this function and return the original value, then you can test it was passed in correctly.

更新您的测试以模拟此函数并返回原始值,然后您可以测试它是否正确传递。

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'
    }
  });
});

#1


5  

So I have figured out a way to do this.

所以我找到了一种方法来做到这一点。

Define a 'private' function on $scope:

在$ scope上定义'private'函数:

$scope._resolve = function(item) {
  return function() {
    return item;
  };
};

Modify the original $scope function to call this 'private' method:

修改原始$ scope函数以调用此'private'方法:

$scope.edit = function(name) {
  var modalInstance = $modal.open({
    templateUrl: 'partials/modal.html',
    controller: 'ModalCtrl',
    scope: $scope,
    resolve: {
      name: $scope._resolve(name)
    }
  });
};

Update your tests to mock this function and return the original value, then you can test it was passed in correctly.

更新您的测试以模拟此函数并返回原始值,然后您可以测试它是否正确传递。

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'
    }
  });
});