Mocha'this'在之前和之前都是钩子 [英] Mocha 'this' in before and beforeEach hooks

查看:106
本文介绍了Mocha'this'在之前和之前都是钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Mocha.js 编写的以下测试代码失败。我希望 someVal 在最后一次测试中增加3倍并且等于3。这个问题出现在更复杂的场景中,我在块之前使用外部中的值设置在内部 beforeEach 块中设置另一个。简化案例:

  describe('增加3次',function(){
before(function(){
this.instanceThis = this;
返回this.someVal = 0;
});
beforeEach(function(){
返回this.someVal + = 1;
});
return describe('深入',function(){
before(function(){
return this.someVal + = 1;
}) ;
beforeEach(function(){
return this.someVal + = 1;
});
返回它('将someVal增加到3',函数(){
返回this.someVal.should.equal(3);
});
});
});


解决方案

解释



我不知道任何版本的Mocha会运行您在问题中显示的代码而不会出错。为了使你的代码有效,它必须这样写:

  require(chai)。should(); 

描述('增加3次',函数(){
之前(function(){
this.someVal = 0;
});
beforeEach(function(){
this.someVal + = 1;
});
describe('going deep',function(){
var parent_ctx = this.parent .ctx;
before(function(){
parent_ctx.someVal + = 1;
//上面的行相当于:
// this.test.parent。 ctx.someVal + = 1;
});
beforeEach(function(){
parent_ctx.someVal + = 1;
});
it('has将someVal增加到3',function(){
parent_ctx.someVal.should.equal(3);
//上面的行相当于:
// this.test.parent .parent.ctx.someVal.should.equal(3);
});
});
});

传递给的函数内部描述和函数传递给 describe 块的钩子(之前的 beforeAll 等)的值是一个上下文对象,对于 describe 是相同的所有自己的挂钩(不是嵌套在其中的其他 describe 调用的挂钩)。因此,当您分配给时,它会分配给上下文。如果你想在嵌套调用 describe 中访问这个上下文,或者在测试中你必须走上描述的树和测试对象。<​​/ p>

解决方案



我会以完全相同的方式完成第二次Rikudo建议:使用变量限制在最上面的描述调用。为了完整起见:

  require(chai)。should(); 

描述('增加3倍',函数(){
var someVal;
before(function(){
someVal = 0;
} );
beforeEach(function(){
someVal + = 1;
});
描述('更深入',函数(){
之前(函数) (){
someVal + = 1;
});
beforeEach(function(){
someVal + = 1;
});
it ('已将someVal增加到3',函数(){
someVal.should.equal(3);
});
});
});


The following test code, written using Mocha.js fails. I expect the someVal to be increased 3 times and equal 3 in the last test. The issue came up in more complex scenario where I used value set in outer before block to set up another in inner beforeEach block. Simplified case:

describe('increasing 3 times', function() {
  before(function() {
    this.instanceThis = this;
    return this.someVal = 0;
  });
  beforeEach(function() {
    return this.someVal += 1;
  });
  return describe('going deeper', function() {
    before(function() {
      return this.someVal += 1;
    });
    beforeEach(function() {
      return this.someVal += 1;
    });
    return it('has increased someVal to 3', function() {
      return this.someVal.should.equal(3);
    });
  });
});

解决方案

Explanation

I don't know of any version of Mocha that would run the code you show in your question without error. For your code to work, it would have to be written like this:

require("chai").should();

describe('increasing 3 times', function() {
    before(function() {
        this.someVal = 0;
    });
    beforeEach(function() {
        this.someVal += 1;
    });
    describe('going deeper', function() {
        var parent_ctx = this.parent.ctx;
        before(function() {
            parent_ctx.someVal += 1;
            // The line above is equivalent to this:
            // this.test.parent.ctx.someVal += 1;
        });
        beforeEach(function() {
            parent_ctx.someVal += 1;
        });
        it('has increased someVal to 3', function() {
            parent_ctx.someVal.should.equal(3);
            // The above line is equivalent to this:
            // this.test.parent.parent.ctx.someVal.should.equal(3);
        });
    });
});

Inside the function passed to describe and the functions passed to the hooks of a describe block (before, beforeAll, etc.) the value of this is a "context" object which is the same for the describe and all of its own hooks (not the hooks of other describe calls nested in it). So when you assign to this, it assigns to the context. If you want to access this context inside nested calls to describe or in tests you have to walk up the tree of describe and test objects.

Solution

I would do it exactly the same way Second Rikudo suggested: use a variable scoped to your uppermost describe call. For the sake of completeness:

require("chai").should();

describe('increasing 3 times', function() {
    var someVal;
    before(function() {
        someVal = 0;
    });
    beforeEach(function() {
        someVal += 1;
    });
    describe('going deeper', function() {
        before(function() {
            someVal += 1;
        });
        beforeEach(function() {
            someVal += 1;
        });
        it('has increased someVal to 3', function() {
            someVal.should.equal(3);
        });
    });
});

这篇关于Mocha'this'在之前和之前都是钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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