如何为ngdialog preclosecallback编写测试用例 - jasmine karma

时间:2017-06-23 11:16:40

标签: angularjs jasmine karma-jasmine

how to write unit test case for ngdialog preclosecallback event 

how to write test case for ngdialog

dialog = ngDialog.openConfirm({                                     范围:范围,      preCloseCallback:function(){                                         $ rootScope.myvariable = false;                                     }     });

1 个答案:

答案 0 :(得分:0)

我猜你很好奇如何测试回调函数。请记住,您可以访问它作为传递给您正在监视的方法的参数。由于你有一个句柄,你可以执行它,并像你要测试的任何其他方法一样测试它。

你应该能够从以下开始,但如果是我,我可能会把它分成多个测试......

it('should call ngDiag.openConfirm()', inject(function($rootScope, ngDialog) {
  // Arrange
  var expectedScope = getExpectedScope();
  $rootScope.myvariable = true;
  spyOn(ngDialog, 'openConfirm')

  // Act
  scope.executeCodeOrWhateverTriggersItToRun();

  // Assert
  expect(ngDialog.openConfirm.calls.count()).toBe(1);

  var args = ngDialog.openConfirm.calls.argsFor(0);
  expect(args).not.toBe(null);
  expect(args.length).toBe(1);

  var options = args[0];
  expect(options.scope).toBe(expectedScope);
  expect(typeof options.preCloseCallback).toBe('function');

  options.preCloseCallback()
  expect($rootScope.myvariable).toBe(false);
}));