如何使用Jasmine在单元测试中调用ngDialog.open

时间:2016-06-17 18:53:42

标签: javascript angularjs unit-testing jasmine karma-jasmine

我使用Jasmine / Karma进行单元测试并对这些框架不熟悉。在控制器中我使用ngDialog进行模型显示,我想要控制器调用ngDialog的单元测试用例。 的控制器:

(function () {
    'use strict';
    angular.module('app')
      .controller('myController', MyController);

    CongratulationsController.$inject = ['$scope',
                                         '$rootScope',
                                         'ngDialog'];

    function CongratulationsController($scope, $rootScope, ngDialog) {

      $scope.myData = {name: 'test',
                      grade: '5'};

      $scope.modal1 = function() {
        ngDialog.open({ template: 'views/modal/modal-1html',
          className: 'ngdialog-theme-default',
          controller: 'ModalController',
          scope: $scope});
      };
      $scope.modal2 = function() {
        ngDialog.open({ template: 'views/modal/modal-2html',
          className: 'ngdialog-theme-default',
          controller: 'ModalController',
          scope: $scope});
      };
    }
}());

这是我的单元测试



'use strict';

describe('Controller: MyController', function () {
  var MyController,location, scope, ngDialogInstance;
  ngDialogInstance = {
    open: jasmine.createSpy('ngDialogInstance.open'),
    dismiss: jasmine.createSpy('modalInstance.dismiss')
  };
    // load the controller's module
    beforeEach(module('app'));
    // Initialize the controller and a mock scope
    beforeEach(inject(function ($controller, $location, $rootScope, _ngDialog_) {
      scope = $rootScope.$new();
        MyController= $controller('myController', {$scope: scope ,
          _ngDialog_: ngDialogInstance
          });
        location = $location;
        spyOn(scope, 'openModal1');
    }));
    it('test controller exists', function () {
        expect(!!MyController).toBe(true);
    });
    
  it('test controller calls ng dialog when it calls openModel method', function () {
    scope.openModal1();
    expect(scope.openModal1).toHaveBeenCalled();
    expect(ngDialogInstance.open).toHaveBeenCalled();
  });
});




当我在测试用例上运行时,低于错误。 它在预期的间谍ngDialogInstance.open失败了。

任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

你必须模仿ngDialogInstance.open方法。所以改变你的这部分代码

it('test controller calls ng dialog when it calls openModel method', function () { scope.openModal1(); expect(scope.openModal1).toHaveBeenCalled(); expect(ngDialogInstance.open).toHaveBeenCalled(); });

进入这个

it('test controller calls ng dialog when it calls openModel method', function () { spyOn(ngDialogInstance, 'open'); scope.openModal1(); expect(scope.openModal1).toHaveBeenCalled(); expect(ngDialogInstance.open).toHaveBeenCalled(); });

注意:spyOn监视你要测试的方法,它是否调用它已被调用,是否使用正确的参数调用等等。

有关spyOn的更多信息,请访问jasmine

的官方文档

答案 1 :(得分:0)

这应该是,

it('test controller calls ng dialog when it calls openModel method', function () {
    ngDialogInstance.open();
    scope.openModal1();
    expect(scope.openModal1).toHaveBeenCalled();
    expect(ngDialogInstance.open).toHaveBeenCalled();
});

您必须包含以下内容:

ngDialogInstance.open();

答案 2 :(得分:0)

如果您想知道为什么必须像这样调用open函数

ngDialogInstance.open();

,因为您需要模仿用户与对话框的交互,例如close,open等。