[英]mocha pass variable to the next test
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();
});
});
以上目前不适用于摩卡。
解决方案是在测试之间共享变量,如上所示。
使用async.waterfall()
这是非常可能的,我真的很喜欢它。 有没有办法让它在摩卡中发生?
谢谢!
最好保持测试隔离,以便一次测试不依赖于在另一次测试中执行的计算。 让我们调用应该通过值测试A的测试和应该测试的测试B.需要考虑的一些问题:
测试A和测试B真的是两个不同的测试吗? 如果没有,他们可以合并。
测试A是否意味着为测试B提供一个测试夹具? 如果是这样,测试A应该成为before
或beforeEach
调用的回调。 您基本上通过将数据分配给describe
的闭包中的变量来传递数据。
describe('some test', function(){ var fixture; before(function(done){ fixture = ...; done(); }); it('do something', function(done){ fixture.blah(...); done(); }); });
我已经阅读过Mocha的代码了,如果我没有忘记某些东西,就没有办法调用describe
, it
或者done
回调来传递值。 所以上面的方法是它。
非常同意路易斯所说的,这就是摩卡实际上不支持它的原因。 想想你引用的异步方法; 如果您的第一次测试失败,则会在其他测试中遇到瀑布故障。
正如你所说,你唯一的方法就是在顶部贴一个变量:
describe('some test', function(){
var value = 0;
it('should pass a value', function(done){
value = 5;
done();
});
it('and then double it', function(done){
console.log(value * 2); // 10
done();
});
});
也可以添加到套装或上下文对象。
在这个例子中,它被添加到suit对象中
describe('suit', function(){
before(() => {
this.suitData = 'suit';
});
beforeEach(() => {
this.testData = 'test';
});
it('test', done => {
console.log(this.suitData)// => suit
console.log(this.testData)// => test
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.