【问题标题】:Submitting a form within Karma process => Some of your tests did a full page reload在 Karma 流程中提交表单 => 您的一些测试完成了整个页面的重新加载
【发布时间】:2013-12-13 11:58:46
【问题描述】:

在一个角度指令中,我有以下代码:

$('[name=' + formName + ']').bind('submit', function () {
   validate();
});

在 Karma 测试的 beforeEach 子句中,我有以下代码:

bootstrapInput = $compile('<form novalidate name="aForm">' +
            '<input-field icon="true" for="email">' +
            '<div>' +
            '<input class="form-control" class="email" name="email" id="email" type="email" ng-model="user.email" required />' +
            '</div>' +
            '<input-validation for="email" custom-error="custom error" required="Email is required" email="Email must be in valid format"/>' +
            '</input-field>' +
            '<button type="submit" value="valider" ></button>' +
            '</form>')($rootScope);

在我的单元测试中,我有这个代码:

it('should launch validation process if form has just been submitted', function(){
            bootstrapInput.submit();  //way of doing?
            expect(bootstrapInput.children().hasClass('has-error')).toBe(true);
});

但我收到以下错误:

Some of your tests did a full page reload!

问题是:如何在 Karma 单元测试中触发提交事件以进行处理,而不需要重新加载页面?

【问题讨论】:

  • 你为什么不直接使用ng-submit

标签: angularjs angularjs-directive karma-runner


【解决方案1】:

同样的问题,但我通过单击提交按钮来解决问题。

bootstrapInput.find('input[type="submit"]').click();
expect(bootstrapInput.children().hasClass('has-error')).toBe(true);

希望有人能找到对此的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2016-03-17
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多