如何确保 mocha 测试中的 'this' 可以访问 class 属性
How to make sure 'this' inside mocha test have access to class properties
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 inside describe points to complete different context,如何使 x 可用于 this inside mocha test
在函数中使用箭头函数 () => this...
或 .bind
。
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});
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 inside describe points to complete different context,如何使 x 可用于 this inside mocha test
在函数中使用箭头函数 () => this...
或 .bind
。
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});