Angularjs - 如何对单元测试表单进行验证 [英] Angularjs - how to unit test form validation
问题描述
我正在使用 Jasmine 对我的Angular App进行单元测试。如何在控制器中测试表单验证?例如,我有一个登录功能:
I'm using Jasmine to unit test my Angular App. How can I test form validation in my controller? For example, I have a login function:
$scope.login = function() {
if($scope.form_login.$valid) {
//send request with username and password
};
};
我正在尝试设置 $ valid
到 true
,但我无法访问此处的表单。我收到错误 TypeError:无法设置未定义的属性'$ valid'
:
I'm trying to set $valid
to true
, but I can't access the form here. I got an error TypeError: Cannot set property '$valid' of undefined
:
it('should not send request if form validation fails', function() {
$controller('loginController', {$scope: $scope});
$scope.form_login.$valid = true;
$scope.login();
})
推荐答案
单元测试不应该真正测试表单。要测试表单和其他非控制器相关的东西,请使用e2e测试。
Unit test should not really test the form. To test the form and other not controller related things use e2e testing.
但是如果你真的想在单元测试中测试验证,请参阅测试自定义验证angularjs指令或如何单位测试angularjs形式?
However if you really want to test validation in unit test see To test a custom validation angularjs directive or How to unit test angularjs form?
这篇关于Angularjs - 如何对单元测试表单进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!