最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to unit test the 'resolve' property on an Angular-UI Bootstrap Modal component - Stack Overflow

programmeradmin0浏览0评论

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 ponents. Here is what I have so far:

// 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 ponent to work correctly - previously I had tried resolve: { name: name } but this didn't work.

// 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:

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?

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 ponents. Here is what I have so far:

// 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 ponent to work correctly - previously I had tried resolve: { name: name } but this didn't work.

// 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:

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?

Share Improve this question asked Apr 17, 2014 at 8:58 Tom SpencerTom Spencer 8,0774 gold badges58 silver badges51 bronze badges 1
  • maybe you could see this: stackoverflow./questions/26853603/… – Khanh TO Commented Nov 16, 2014 at 3:20
Add a ment  | 

1 Answer 1

Reset to default 6

So I have figured out a way to do this.

Define a 'private' function on $scope:

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论