在 Karma 流程中提交表单 =>你的一些测试做了整页重新加载 [英] Submitting a form within Karma process => Some of your tests did a full page reload

查看:21
本文介绍了在 Karma 流程中提交表单 =>你的一些测试做了整页重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

bootstrapInput = $compile('
' +''+'

'+'<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="电子邮件必须采用有效格式"/>'+'</输入字段>'+'<button type="submit" value="valider" ></button>'+'</form>')($rootScope);

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

it('如果表单刚刚提交,应该启动验证过程', function(){bootstrapInput.submit();//怎么做?期望(bootstrapInput.children().hasClass('has-error')).toBe(true);});

但我收到以下错误:

你的一些测试重新加载了整页!

问题是:如何在不重新加载页面的情况下,在 Karma 单元测试中引发提交事件进行处理?

解决方案

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

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

希望有人对此进行解释.

In one angular directive, I have this code:

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

In the beforeEach clause of a Karma test, I have this code:

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

And in my unit test, I have this code:

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

But I got the following error:

Some of your tests did a full page reload!

The question is: How to provoke a submit event to process inside a Karma unit test, without making page reload?

解决方案

Same issue but I do a workaround using a click on the submit button.

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

Hope someone will find an explication to this.

这篇关于在 Karma 流程中提交表单 =>你的一些测试做了整页重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆