簡體   English   中英

如何使摩卡咖啡中的“ it”等到“ it”中的承諾得到解決?

[英]How to make the “it” in mocha to wait until the promise within “it” gets resolved?

var promise = require('promise');
var {Builder, By, Key, until} = require('selenium-webdriver');
var test = require('selenium-webdriver/testing');
var chai = require('chai');
var getUrl = require('./wdio.conf.js');
var driver = new Builder().forBrowser('chrome').build();

test.describe('Proper Testing', function() {
    test.it('should prompt the server from user', function() {
        return new promise(function(resolve,reject){
            resolve(driver.get("https://www.google.co.in"));
            reject(err);
        })
    })
}) 

對於上面給出的代碼,運行mochaproperty.js在chrome瀏覽器中打開給定的url,但測試失敗並出現超時錯誤。 我已經讀到,如果測試返回了promise,則無需調用done()。 給定代碼有什么問題?

完成測試后,您必須致電done

我不確定您要在這里實現什么,但是這個示例代碼應該說明我的意思:

describe('Proper Testing', function() {

    it('should prompt the server from user', function(done) {
        return getUrl().then(function(url){
            driver.get(url)
            .then(function () {
              done();
            }).catch(function (err) {
              done(err);
            });
        })
    });

})

參考: https : //mochajs.org/#asynchronous-code

關於您的問題,請嘗試添加一個設置高超時的before語句。 普通測試具有默認超時時間(我認為是2秒),如果測試超過此時間,則會引發錯誤,並且無法通過測試。

before(function (done) {
    this.timeout(5000);
    done();
})

關於完成:如果您要兌現承諾,則無需使用完成。 簡單地聲明不帶任何參數的function(),如果它不引發任何異常並且完成所有斷言,它將發現它通過了測試。

describe('Proper Testing', function() {

    it('should prompt the server from user', function() {
        return getUrl().then(function(url){
            driver.get(url);
        })
    });

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM