简体   繁体   English

Javascript:如何将方法附加到已定义的类型

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

I'm currently using "Intern.js" to do functional/behavior tests for my front-end application(click on buttons, expect a message to pop-up and so on). 我目前正在使用“ Intern.js”为前端应用程序进行功能/行为测试(单击按钮,希望弹出一条消息,依此类推)。

A simple test would be something like: 一个简单的测试将是这样的:

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

Note that this part of the test: 请注意,这部分测试:

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

Would be repeated for every input on the modal, so instead of repeating it on every test, I wanted to do something like: 对于模态上的每个输入都将重复执行此操作,因此我不想在每个测试中都重复执行此操作,例如:

/** 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();

And the "must.be.a.text.input()" would have the type assertions. 并且“ must.be.a.text.input()”将具有类型断言。

A important thing to notice is that all those "Intern.js" methods returns a Promise. 需要注意的重要一点是,所有这些“ Intern.js”方法都将返回一个Promise。

Do you guys have any suggestions? 你们有什么建议吗?

Thanks! 谢谢!

The "Intern.js" lib: https://theintern.github.io/ “ Intern.js”库: https ://theintern.github.io/

It's documentation: https://theintern.github.io/leadfoot/module-leadfoot_Command.html 它是文档: https : //theintern.github.io/leadfoot/module-leadfoot_Command.html

You can try to use chai-as-promised . 您可以尝试使用chai-as-promised

With chai-as-promised, your code 遵循承诺,您的代码

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

can be written as 可以写成

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