Mocha-如何使用before块设置变量?

问题描述 投票:0回答:1

我的例子有

let resort30 = new Resort('Alta');

在文件顶部,然后在测试用例中使用它,即

let alta = new Visit(resort30, '03/01/2000'); // Note this is before any describe

行得通。但是,如果我在before内和所有beforeEach之前创建describeit,请使用:

before(function () {
  let resort30 = new Resort('Alta');
});

并注释掉原始声明,然后resort30无法识别

 ReferenceError: resort30 is not defined                                                                                                  
  at Context.<anonymous> (test/ski_resorts_using_befores.spec.js:52:26)  

[将变量移到beforebeforeEach以便所有it都可以使用它时,如何使它工作?

javascript mocha chai
1个回答
0
投票

您看不到resort30,因为如果在函数中声明它,则该作用域仅作用于该函数。

let resort30; // declare it at the top level scope of the file

before(function () {
    resort30 = new Resort('Alta'); // modify the value here
});
© www.soinside.com 2019 - 2024. All rights reserved.