mocha将变量传递给下一个测试 [英] mocha pass variable to the next test

查看:115
本文介绍了mocha将变量传递给下一个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

describe('some test', function(){
    // Could put here a shared variable
    it('should pass a value', function(done){
        done(null, 1);
    });
    it('and then double it', function(value, done){
        console.log(value * 2);
        done();
    });
});

上述目前不适用于摩卡。

The above currently would not work in mocha.

解决方案是在测试之间共享变量,如上所示。

A solution would be to have a variable shared between the tests, as shown above.

使用 async.waterfall()这很可能,我真的很喜欢。有什么方法可以让它在摩卡中发生吗?

With async.waterfall() this is very possible and I really like it. Is there any way to make it happen in mocha?

谢谢!

推荐答案

最好保持测试隔离,以便一次测试不依赖于在另一次测试中执行的计算。让我们调用应该通过值测试A的测试和应该测试的测试B.需要考虑的一些问题:

It is much preferable to keep the tests isolated so that one test does not depend on a computation performed in another. Let's call the test that should pass a value test A and the test that should get it test B. Some question to consider:


  1. 测试A和测试B真的是两个不同的测试吗?如果没有,它们可以合并。

  1. Are test A and test B really two different tests? If not, they could be combined.

测试A是否意味着为测试B提供一个测试夹具?如果是这样,测试A应该成为 beforeEach 之前的的回调。您基本上通过将数据分配给 describe 的闭包中的变量来传递数据。

Is test A meant to provide test B with a fixture to test against? If so, test A should become the callback for a before or beforeEach call. You basically pass the data around by assigning it to variables in the closure of describe.

describe('some test', function(){
    var fixture;

    before(function(done){
        fixture = ...;
        done();
    });

    it('do something', function(done){
        fixture.blah(...);
        done();
    });
});


我读过Mocha的代码,并提供我没有忘记什么,没有办法打电话给描述,或 done 回调传递值。所以上面的方法就是它。

I've read Mocha's code, and provided I'm not forgetting something, there is no way to call describe, it, or the done callback to pass values around. So the method above is it.

这篇关于mocha将变量传递给下一个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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