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

查看:35
本文介绍了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();
    });
});

以上目前在 mocha 中不起作用.

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() 这很有可能,我真的很喜欢它.有没有办法让它在 mocha 中发生?

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

谢谢!

推荐答案

最好保持测试隔离,这样一个测试就不会依赖于另一个测试中执行的计算.让我们将应该通过值测试 A 的测试和应该通过值测试 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 应该成为 beforebeforeEach 调用的回调.您基本上通过将数据分配给 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 的代码,并且只要我没有忘记某些东西,就无法调用 describeitdone 回调来传递值.所以上面的方法就是它了.

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