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来完成不同的上下文,如何使x可用于mocha测试中的this
最佳答案
在函数上使用箭头函数 () => this...
或 .bind
。
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});
关于javascript - 如何确保 mocha 测试中的 'this' 可以访问类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60212359/