Validation 如何在Angular中测试表单验证
我试图围绕使用Angular的表单编写测试 在完成以下操作之后,我能够在e2e测试中访问表单的作用域。现在使用此代码:Validation 如何在Angular中测试表单验证,validation,angularjs,testing,jasmine,end-to-end,Validation,Angularjs,Testing,Jasmine,End To End,我试图围绕使用Angular的表单编写测试 在完成以下操作之后,我能够在e2e测试中访问表单的作用域。现在使用此代码: scope('Form', function(scope) { scope.email = "test@test.com"; scope.password = "abcd1234"; expect(scope.form.$valid).toBe(true); }) 无论出于何种原因,scope.form.$valid为false。如果我将其包装在一个
scope('Form', function(scope) {
scope.email = "test@test.com";
scope.password = "abcd1234";
expect(scope.form.$valid).toBe(true);
})
无论出于何种原因,scope.form.$valid
为false。如果我将其包装在一个setTimeout()
中,那么它工作得非常好。Angular的sleep()
方法没有用
有什么指针吗?你应该
$digest
在最后一个scope.password之后添加scope.$digest()
。多亏了mimo的建议,我找到了最终的解决方案
为了愚弄expect
,让未来成为现实,必须构造一个定制的dsl语句
angular.scenario.dsl('expectScope', function() {
var _retrieve = function(source, target) {
if(target == '') return source;
var targets = target.split('.');
var nextTarget = targets[0];
return _retrieve(source[nextTarget], targets.splice(1).join('.') );
};
var chain = angular.extend({}, angular.scenario.matcher);
chain.not = function() {
this.inverse = true;
return chain;
};
return function(scope, name) {
this.future = new angular.scenario.Future('scope.'+name, function() {});
this.future.value = _retrieve(scope, name);
return chain;
};
});
通过在
expectScope(scope,'value.othervalue')
之前调用scope.$digest
,现在一切都按预期进行了。很抱歉,还有一些步骤需要完成,但您是正确的。谢谢