在一个角度指令中,我有这段代码:
$('[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单元测试中处理,而不进行页面重新加载?
答案 0 :(得分:2)
同样的问题,但是我使用点击提交按钮来解决方法。
bootstrapInput.find('input[type="submit"]').click();
expect(bootstrapInput.children().hasClass('has-error')).toBe(true);
希望有人能找到解释。