2016-09-13 26 views
0

我目前使用「Intern.js」爲我的前端應用程序做功能/行爲測試(點擊按鈕,期望彈出消息等等上)。Javascript:如何將方法附加到已定義的類型

一個簡單的測試會是這樣的:

bdd.describe('###### txtFirstName', function() { 
    bdd.it('must be a "text" type input', function() { 
    /** Begin the test */ 
    let test = 

     /** Find the 'addUserModal' on the DOM and then find the 'txtFirstName' inside it */ 
     helper.addUserModal.find.field.txtFirstName() 
     .getProperty('type') 
     .then(function(type) { 
      expect(type).to.equal('text'); 
     }) 
     .end() 
    /** End the test */ 
    .end(); 

    return test; 
    }); 
}); 

注意的是,這部分測試:

.getProperty('type') 
.then(function(type) { 
    expect(type).to.equal('text'); 
}) 
.end() 

將重複在模式每個輸入,所以不是在重複它每一個測試,我想做的事情如下:

/** Begin the test */ 
let test = 
    /** Find the 'addUserModal' on the DOM and then find the 'txtFirstName' inside it */ 
    helper.addUserModal.find.field.txtFirstName() 
    .must.be.a.text.input() 
/** End the test */ 
.end(); 

而「must.be.a.text.input()」將有e型斷言。

需要注意的一件重要事情是,所有那些「Intern.js」方法都返回一個Promise。

你們有什麼建議嗎?

謝謝!

的 「Intern.js」 LIB: https://theintern.github.io/

它的文檔: https://theintern.github.io/leadfoot/module-leadfoot_Command.html

回答

0

你可以嘗試使用chai-as-promised

隨着柴作爲許諾的,你的代碼

helper.addUserModal.find.field.txtFirstName() 
    .getProperty('type') 
    .then(function(type) { 
     expect(type).to.equal('text'); 
    }) 

可以寫成

expect(helper.addUserModal.find.field.txtFirstName().getProperty('type')) 
    .to.eventually.equal('text') 
相關問題