在javascript中测试时,'before'块中定义的变量在'It'块中使用时显示未定义



这是一个样本测试,我正在测试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);
})
}

相关内容

最新更新