2016-08-25 65 views
1

我正在使用Mocha/Chai進行量角器測試套件(是的,我在Jasmine中遇到同樣的問題)。爲什麼Mocha不執行.then語句中的測試?

因爲這個應用程序相當複雜,所以我希望能夠干涉設置測試套件,這些測試套件允許我將操作鏈接到功能中。 (即,「登錄,然後瀏覽到[parameterX],然後瀏覽到[parameterY],然後期望第一篇文章標題爲[parameterZ])。

但是,我似乎遇到了摩卡運行測試時遇到問題我把他們。於是()語句內。

這裏是一個小的代碼片段,顯示我的意思的行爲。

var chai = require('chai'); 
var cAP = require('chai-as-promised') 
chai.use(cAP); 
const expect = chai.expect; 

const doTest = (x) => { 
    describe('main', function() { 
    it('should return foo', function(done) { 
     expect(x).to.equal('foo') 
    }) 
    }) 
} 

const wait =() => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(), 10000) 
}) 

wait() 
    .then(() => doTest('foo')) 

/* results in 
* 0 passing (4ms) 
*/ 
+0

只是想知道,你有沒有嘗試過沒有運行所有隱含的回報你在這裏,你有沒有試過在將整個東西包裝在描述塊中之後運行它? – Pytth

+0

隱含的回報是有點爲什麼我這樣做。 –

回答

1

describeit塊可能不被異步完成。如果你要等待10秒在執行測試之前,您應該使用支持異步執行的before塊。您的before回調需要一個參數,這是一個done回調。在這個模塊內部,您可以等待10秒,然後完成調用。所有後續塊將等待,直到從before塊中調用done

describe('your tests',() => { 
    before(done => { 
     setTimeout(done, 10000) 
    }) 
    it('should return foo',() => { 
     expect(x).to.equal('foo') 
    }) 
}) 
+0

我很害怕這個。使用「beforeAll」塊的問題是,在運行本質上相同的測試之前,我可能會有不同的異步「beforeAll」路由到不同的頁面。 有沒有辦法使描述和它異步compatable? –

+0

你也可以在'it'塊內運行異步代碼(之後調用'done')。但是我害怕'describe'和'it''不能被異步調用。然後摩卡不知道什麼時候退出。 –

+0

我將不得不分叉摩卡並編寫我自己的測試套件,不是嗎? –

1

問題是您正在異步創建您的測試套件。默認情況下,Mocha不允許異步創建套件。我把你的代碼,並進行了如下修改:

  1. 刪除從傳遞給it,因爲它是無用的回調done參數。

  2. 我在doTest的末尾添加了對run的呼叫。

  3. 我用--delay選項調用摩卡:mocha --delay

--delay選項將使摩卡等待測試套件的建立。您表示您已通過致電run()來完成該任務。下面是上面提到的完整的代碼與變化:

var chai = require('chai'); 
var cAP = require('chai-as-promised') 
chai.use(cAP); 
const expect = chai.expect; 

const doTest = (x) => { 
    describe('main', function() { 
    it('should return foo', function() { 
     expect(x).to.equal('foo') 
    }) 
    }) 

    run(); 
} 

const wait =() => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(), 10000) 
}) 

wait() 
    .then(() => doTest('foo')) 
0

嗯,我無法找到一個確切的答案,但我能找到的東西,還挺會爲我真的需要這個工作,所以,這是我所做的。

本質上,所有其他異步操作都可以作爲參數傳遞給測試函數,並且可以使用before塊的done()回調來確保它們在正確的時間執行。

const wait5 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 5), 5000) 
}) 

const wait10 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 10), 10000) 
}) 

const doTest = (x, y, prep) => { 
    describe('main', function() { 
    let z; 
    before(function (done) { 
     this.timeout(30000); 
     prep(y).then((val) => { 
     z = val; 
     done(); 
     }) 
    }) 
    it('should return the right number for ' + x + ' and ' + y,() => { 
     expect(x).to.equal(z) 
    }) 
    }) 
} 

[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing)) 


/* Result: 

    main 
    √ should return the right number for 17 and 12 

    main 
    √ should return the right number for 15 and 5 

    main 
    1) should return the right number for 15 and 5 


    2 passing (20s) 
    1 failing 

    1) main should return the right number for 15 and 5: 

     AssertionError: expected 15 to equal 10 
     + expected - actual 

     -15 
     +10 
*/ 
+0

不要放棄chai-as-promised,但要小心箭頭功能。我把你的解決方案進一步... –

1

柴作爲-承諾可以把它一點點清潔

/** 
* Modified by cool.blue on 26-Aug-16. 
*/ 
'use strict'; 
var chai = require('chai'); 
var cAP = require('chai-as-promised'); 
chai.use(cAP); 
const should = chai.should(); 

const wait5 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 5), 5000) 
}); 

const wait10 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 10), 10000) 
}); 

const doTest = (x, y, prep) => { 
    describe('main', function() { 
    this.timeout(15000); 
    it('should return the right number for ' + x + ' and ' + y, function() { 
     return prep(y).should.become(x) 
    }) 
    }) 
}; 

[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing)); 

但是,要小心的箭頭的功能:他們對待語句和表達式不同。

it('should return the right number for ' + x + ' and ' + y, 
    () => prep(y).should.become(x) 
    ) 

it('should return the right number for ' + x + ' and ' + y,() => { 
    prep(y).should.become(x) 
}) 

完全不同的,但正是因爲

it('should return the right number for ' + x + ' and ' + y,() => { 
    return prep(y).should.become(x) 
}) 

箭頭的功能讓我緊張一樣的,這就是爲什麼我喜歡...

it('should return the right number for ' + x + ' and ' + y, function() { 
    return prep(y).should.become(x) 
}) 
相關問題