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

标签 javascript angularjs unit-testing jasmine karma-jasmine

我正在使用 Jasmine/Karma 进行单元测试,并且是这些框架的新手。在 Controller 中,我使用 ngDialog 进行模型显示,并且我想要 Controller 调用 ngDialog 的单元测试用例。 Controller :

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

当我运行以上测试用例时,出现以下错误。 它在预期 spy ngDialogInstance.open 被调用时失败。

谁能帮我解决这个问题吗?

最佳答案

这应该是,

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();

关于javascript - 如何使用 Jasmine 在单元测试中调用 ngDialog.open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888517/

相关文章:

javascript - 在函数中传递方法

javascript - ng-repeat angularjs 1.5.11 不起作用

javascript - 为什么我可以在 `ng-bind` 之外使用 `ng-app` ?

javascript - AngularJS - $rootScope 属性值在服务中未定义

php - 模拟数据库查询 laravel mock

c# - JSON.NET 序列化在 ApiController 中的行为不同

javascript - 使用 d3 js 库的 data 方法将事件绑定(bind)到动态生成的内容

Javascript原生排序方法代码

javascript - 在没有更改 href 详细信息的链接上单击(Ctrl + 单击)时停止打开新选项卡

android - 运行单元测试用例时达到 Multidex 限制