簡體   English   中英

Javascript:如何將方法附加到已定義的類型

[英]Javascript: How to attach methods to a already defined type

我目前正在使用“ Intern.js”為前端應用程序進行功能/行為測試(單擊按鈕,希望彈出一條消息,依此類推)。

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

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()”將具有類型斷言。

需要注意的重要一點是,所有這些“ Intern.js”方法都將返回一個Promise。

你們有什么建議嗎?

謝謝!

“ Intern.js”庫: https ://theintern.github.io/

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

您可以嘗試使用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')

暫無
暫無

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

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