变量范围在 Mocha 测试框架中是如何工作的? [英] How does variable scope work within the Mocha test framework?

查看:19
本文介绍了变量范围在 Mocha 测试框架中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 javascript、node.js、mocha 等所有东西的相对新手.

I am a relative newbie to all things javascript, node.js, mocha etc.

在我的代码中,我有一个 Unit 对象,它有一个 disable() 将禁用属性设置为 true 和一个 isDisabled() 返回禁用属性.它还有一个方法 nextTurnReset() 可以在下一回合开始时重置单位.我编写了一个测试套件来测试这种行为.我首先禁用该对象,然后尝试测试它是否被禁用.然而,我的第一个测试中的单元变量——它在传递给 Mocha 的 it() 方法的匿名函数内——处于非禁用状态,正如我用节点的调试器观察到的那样.

In my code I have a Unit object that has a disable() that sets the disabled property to true and a isDisabled() that returns the disabled property. It also has a method nextTurnReset() that resets the unit on the start of the next turn. I have written a test suite to test this behavior. I first disable the object and then try to test to see if it is disabled. However, the unit variable inside my first test - which is within the anonymous function passed to the Mocha's it() method - is in the the non-disabled state as I observed with node's debugger.

describe('#disable()', function() {
    var unit = tests.newUnit();
    unit.disable();
    debugger;
    it('disabled off turn?', function() {
        debugger;
        (unit.isDisabled()).should.be.exactly(true);
    });
    unit.nextTurnReset();
    it('disabled on next turn?', function() {
        (unit.isDisabled()).should.be.exactly(true);
    });
    unit.nextTurnReset();
    it('disabled on 2nd turn?', function() {
        (unit.isDisabled()).should.be.exactly(false);
    });
});

作为记录,前两个测试失败,最后一个成功,表明该单元从未被禁用.

for the record, the first two tests fail, and the last one succeeds indicating the unit is never disabled at all.

从使用节点调试器的repl:在第一个debugger;语句之后,unit.disabled == true,但在第二个debugger;之后> 语句 unit.disabled == false.我希望在这两种情况下该值都为真.

from using the node debugger's repl: After the first debugger; statement, unit.disabled == true, but after the second debugger; statement unit.disabled == false. I expect the value to true in both cases.

知道为什么会这样吗?另外,编写 Mocha 测试以获得预期结果的正确方法是什么?

Any idea why this would be the case? Also, what is the correct way of writing Mocha tests to get my expected result?

非常感谢!

推荐答案

Mocha 中的变量作用域与任何其他 JavaScript 代码中的完全相同.您的问题是您没有意识到 Mocha 将按照什么顺序执行您的代码.事情是这样的:

Variable scoping in Mocha is exactly the same as in any other JavaScript code. Your problem is that you do not realize in which order Mocha will execute your code. This is what happens:

  1. 您创建单元实例并对其调用 disable.

您注册了您的第一个测试.这就是it 所做的:它为未来 执行注册一个测试.测试现在不执行.

You register your first test. That's what it does: it registers a test for future execution. The test does not execute now.

您调用 unit.nextTurnReset(); 以重置对象的状态.

You call unit.nextTurnReset(); which resets your object's state.

您注册了第二个测试.再次现在不执行.

You register your second test. Again it does not execute now.

您再次重置对象.

您注册了上次测试.

此后,Mocha 接受您注册的测试并运行它们.当您的测试运行时,您的对象处于重置状态,而不是禁用状态.

After this, Mocha takes the tests you registered and runs them. By the time your tests are running your object is in its reset state, not disabled.

在我看来,鉴于您描述的所需行为,您的代码应该是:

It seems to me that given the desired behavior you describe, your code should be:

describe('#disable()', function() {
    var unit = tests.newUnit();

    beforeEach(function () {
        unit.nextTurnReset();
    });

    it('disabled off turn?', function() {
        unit.disable();
        (unit.isDisabled()).should.be.exactly(true);
    });

    it('disabled on next turn?', function() {
        (unit.isDisabled()).should.be.exactly(false);
    });
});

传递给 beforeEach 的代码在您注册的每个测试之前运行,因此它会在正确的时间重置对象.

The code passed to beforeEach is run before each test you've registered so it resets the object at the right time.

这篇关于变量范围在 Mocha 测试框架中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆