【问题标题】:AngularJS: How to write Jasmine test for form submit?AngularJS:如何为表单提交编写 Jasmine 测试?
【发布时间】:2016-09-16 18:07:39
【问题描述】:

我想自动提交指令模板中的表单。以下是相同的指令代码:

link: function(scope, el) {
    $timeout(function() {
       el.submit();
    });
}

如何为这段代码编写 Jasmine 测试?

【问题讨论】:

    标签: angularjs angularjs-directive submit form-submit karma-jasmine


    【解决方案1】:

    我能够通过监视提交函数并将调用委托给提供的函数来解决我的问题。

    这在 beforeEach 块中:-

    element = angular.element(html);
    compiledDirective = _$compile_(element)($scope);
    $scope.$digest();
    form = element[0];
    spyOn(form, 'submit').and.callFake(function() {
        return false;
    });
    

    然后在 it() 函数中测试你的代码提交:-

    it('check if it submits the form', function() {
        $timeout.flush();
        expect(form.submit).toHaveBeenCalled();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2014-09-15
      • 2019-03-13
      相关资源
      最近更新 更多