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

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

问题描述

我是javascript,node.js,mocha等所有事物的新手.

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

在我的代码中,我有一个Unit对象,该对象具有一个disable(),它将属性设置为true,而isDisabled()则返回了Disabled属性.它还具有方法nextTurnReset(),该方法可在下一回合开始时重置设备.我已经编写了一个测试套件来测试这种行为.我首先禁用该对象,然后尝试进行测试以查看其是否被禁用.但是,在我的第一个测试中的unit变量(位于传递给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.

:在第一个debugger;语句unit.disabled == true之后,但在第二个debugger;语句unit.disabled == false之后.我希望在两种情况下该值都为true.

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.

您再次重置了对象.

您注册您的最后一个测试.

You register your last test.

此后,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天全站免登陆