这是一个样本测试,我正在测试testNum应该是0,
代码:
describe("Test Contract", () => {
before(async () => {
const testNum = 0;
})
it("should be zero", function () {
expect(testNum).to.equal(0);
})
}
但我收到一个错误,说testNum未定义。
错误:
1) Test Contract
should be zero:
ReferenceError: testNum is not defined
我做错了什么?
变量testNum
的作用域为您定义它的{}。此外,const
用于只读值。我想您要多次重新分配testNum
。因此,您应该使用let
。
你可能想要的是:
describe("Test Contract", () => {
let testNum;
before(async () => {
testNum = 0;
})
it("should be zero", function () {
expect(testNum).to.equal(0);
})
}