将分配保留在原处,但将其包装在
beforeEach回调中,您的代码将执行:
beforeEach(function () { accountToPost.password_confirm = 'something';});Mocha会加载并执行文件,这意味着
describe调用会 在 Mocha实际运行测试套件 之前立即 执行。这样便可以计算出已声明的测试集。
我通常只将函数和变量声明放在传递给的回调主体中
describe。这一切都 改变了
用于测试对象的状态属于在
before,
beforeEach,
after或
afterEach,或在测试内部自己。
另一件事知道的是,
beforeEach与
afterEach之前和回调后执行
it的呼叫 没有
回调到
describe呼叫。因此,如果您认为您的
beforeEach回调将在
describe('POST /account/register',...不正确之前执行。它在之前执行it('returns error', ...。此代码应说明我在说什么:
console.log("0");describe('level A', function () { console.log("1"); beforeEach(function () { console.log("5"); }); describe('level B', function(){ console.log("2"); describe('level C', function(){ console.log("3"); beforeEach(function () { console.log("6"); }); it('foo', function () { console.log("7"); }); }); });});console.log("4");如果在此代码上运行mocha,您将看到数字以递增顺序输出到控制台。我已经按照测试套件的构建方式进行了结构化,但是添加了我建议的修复程序。当Mocha找出套件中存在哪些测试时,将输出数字0到4。测试尚未开始。其他数字在正确测试期间输出。



