2015-01-21 19 views
5

Trong chương trình của tôi, tôi đang tính toán hai con số, và tôi muốn chắc chắn rằng trừ trong số họ bằng 1.thước đo - so sánh số

này là mã:

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

cho đến nay rất tốt - Tôi đang lấy số. vấn đề xuất phát tiếp theo:

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

Tôi nhận được lỗi này:

Expected NaN to equal 1. 

bất kỳ ý tưởng?

Trả lời

4

Cả firstCountsecondCountlời hứa được cần thiết để được giải quyết:

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

Tôi vẫn đang cố gắng hiểu khái niệm về 'lời hứa' nhưng nó hoạt động! cảm ơn rất nhiều. – user2880391

0

Có thể giải quyết chỉ lời hứa đầu tiên. Thước đo thích hợp expect để "hiểu" lời hứa. Tham khảo 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); 
}) 

});

0

Bạn đang làm hoàn toàn đúng. Nhưng Trước khi so sánh, hãy kiểm tra xem liệu giá trị kết quả của bạn có phải là loại số hay không.

Example-

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

Sau đó thực hiện một hoạt động cho điều kiện mong đợi.