【问题标题】:How to make sure 'this' inside mocha test have access to class properties如何确保 mocha 测试中的“this”可以访问类属性
【发布时间】:2020-02-13 16:20:38
【问题描述】:

const expect = require("chai").expect;

class Test 
{
 constructor(){ this.x= 10;}
 run() {
 describe("test goes here", function() {
  it("sample test", function() {
    expect(this.x).to.be.eq(10);
  });
 });
 }
}

new Test().run();

得到 x 是未定义的。

问题:this里面的describe指向完成不同的上下文,如何使x可用于摩卡测试中的this

【问题讨论】:

    标签: javascript node.js mocha.js


    【解决方案1】:

    在您的函数上使用箭头函数 () => this....bind

    describe("test goes here", () => {
      it("sample test", () => {
        expect(this.x).to.be.eq(10);
      });
     });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多