如何确保Mocha测试中的"this"可以访问类属性 [英] How to make sure 'this' inside mocha test have access to class properties
本文介绍了如何确保Mocha测试中的"this"可以访问类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
const Expect = require("chai").expect;
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是不确定的.
问题:此内部描述了完成不同上下文的要点,以及如何使x在Mocha测试中可供此使用
Issue : this inside describe points to complete different context, how to make x available to this inside mocha test
推荐答案
在功能上使用箭头功能() => this...
或.bind
.
Use arrow functions () => this...
or .bind
on your functions.
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});
这篇关于如何确保Mocha测试中的"this"可以访问类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文