Pon*_*oni 25 javascript testing mocha.js
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();
});
});
Run Code Online (Sandbox Code Playgroud)
以上目前不适用于摩卡.
解决方案是在测试之间共享变量,如上所示.
随着async.waterfall()这是非常有可能的,我真的很喜欢它.有没有办法让它在摩卡中发生?
谢谢!
Lou*_*uis 55
最好保持测试隔离,以便一次测试不依赖于在另一次测试中执行的计算.让我们调用应该通过值测试A的测试和应该测试的测试B.需要考虑的一些问题:
测试A和测试B真的是两个不同的测试吗?如果没有,他们可以合并.
测试A是否意味着为测试B提供一个测试夹具?如果是这样,测试A应成为a before或beforeEachcall 的回调.您基本上通过将数据分配给闭包中的变量来传递数据describe.
describe('some test', function(){
var fixture;
before(function(done){
fixture = ...;
done();
});
it('do something', function(done){
fixture.blah(...);
done();
});
});
Run Code Online (Sandbox Code Playgroud)我读过摩卡的代码,并提供我不是忘了什么东西,也没有办法打电话describe,it或者done回调到各地传递值.所以上面的方法是它.
whi*_*fin 10
非常同意路易斯所说的,这就是摩卡实际上不支持它的原因.想想你引用的异步方法; 如果您的第一次测试失败,则会在其他测试中遇到瀑布故障.
正如你所说,你唯一的方法就是在顶部贴一个变量:
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();
});
});
Run Code Online (Sandbox Code Playgroud)
也可以添加到西装或上下文对象。
在此示例中,将其添加到西服对象
describe('suit', function(){
before(() => {
this.suitData = 'suit';
});
beforeEach(() => {
this.testData = 'test';
});
it('test', done => {
console.log(this.suitData)// => suit
console.log(this.testData)// => test
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33481 次 |
| 最近记录: |