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:
-
测试A和测试B真的是两个不同的测试吗?如果没有,它们可以合并。
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.