0

我正在尝试围绕使用 Angular 的表单编写测试。

遵循此解决方案后,我可以在 e2e 测试中访问表单的范围。现在使用此代码:

scope('Form', function(scope) {
    scope.email = "test@test.com";
    scope.password = "abcd1234";

    expect(scope.form.$valid).toBe(true);
})

不管什么原因,scope.form.$valid都是假的。如果我将它包装在 asetTimeout()中,它会很好地工作。Angular 的sleep()方法没有用。

任何指针?

4

2 回答 2

2

您应该在最后一个 scope.password 之后$digest添加范围。scope.$digest()

于 2013-12-11T00:54:23.453 回答
0

感谢 mimo 的建议,我得出了最终的解决方案。

必须构造一个自定义的 dsl 语句,以欺骗expect未来。

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

通过调用scope.$digestbefore expectScope(scope, 'value.othervalue'),现在一切都按预期工作。

于 2013-12-11T02:04:32.600 回答