【问题标题】:Protractor - compare numbers量角器 - 比较数字
【发布时间】:2015-01-21 21:16:47
【问题描述】:

在我的程序中,我正在计算两个数字,我想确保它们的减法等于 1。

这是代码:

var firstCount=element.all(by.repeater('app in userApps')).count();
var secondCount=element.all(by.repeater('app in userApps')).count();

到目前为止一切都很好-我得到了数字。问题来了:

var sub=secondCount-firstCount;
expect(sub).toEqual(1);

我收到此错误:

Expected NaN to equal 1.

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs protractor subtraction end-to-end


    【解决方案1】:

    firstCountsecondCount 都是承诺需要解决

    element.all(by.repeater('app in userApps')).count().then(function (first) {
        element.all(by.repeater('app in userApps')).count().then(function(second) {
            expect(first - second).toEqual(1);
        })
    });
    

    【讨论】:

    • 我仍在尝试理解“承诺”的概念,但它确实有效!非常感谢。
    【解决方案2】:

    可以只解决第一个承诺。 Protractor 使expect 适应“理解”承诺。参考https://github.com/angular/protractor/blob/master/docs/control-flow.md#protractor-adaptationshttps://github.com/angular/protractor/issues/128

    element.all(by.repeater('app in userApps')).count().then(function (first) {
        // Do any changes here...
        var second = element.all(by.repeater('app in userApps')).count();
        // Here expect() resolves 'second'
        expect(second).toEqual(first + 1);
    })
    

    });

    【讨论】:

      【解决方案3】:

      你做的绝对正确。但 在比较之前,请检查您的结果值是否为数字类型。

      例子-

      expect(sub).toEqual(jasmine.any(Number));
      

      然后针对预期条件执行操作。

      【讨论】:

        猜你喜欢
        • 2017-09-02
        • 2016-10-17
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多