以此代码为例:
const assert = require("assert");
describe('Force fail after iterations', function () {
var retriesDone = 0;
it('Math.random will be eventually give the same numbers', function () {
this.retries(Infinite);
retriesDone++;
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
assert.equal(a,b);
});
});
这将执行(n)次,然后在我的控制台中提供此结果:
Force fail after iterations
√ Math.random will be eventually give the same numbers
但是我不知道Mocha经历了多少次重复Math.random
次。
在我正在进行的现实世界项目中,如果重试次数少于50次,那么我的功能就会很弱,如果它超过50则那么它很好。所以我需要知道完成了多少次迭代。
我当然可以这样做:
if(a===b) console.log("Retries:",retriesDone);
但它会输出缩进的结果,如下所示:
Force fail after iterations
Retries: 1
√ Math.random will be eventually give the same numbers
我不觉得这是在摩卡进行力量失败测试的最佳方法!有更好的方法吗?
注意:我的意思是力 - 失败测试,就像通过施加(n)Kg然后(n)+ 100Kg,(n)+ 200Kg,(n)的重量来测试飞机机翼阻力时+ 500Kg,(n)+ 900Kg ..等等。
答案 0 :(得分:0)
得到它..对于任何人来到这个线程,检查此代码和评论
describe('Force fail after iterations', function () {
var retriesDone = 0;
it('Math.random will be eventually give the same numbers', function () {
this.retries(Infinite);
retriesDone++;
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
if(a === b) { // when we get an equal values
// if the retries were more than 50, we're good
if(retriesDone>50) assert.equal(h1,h2);
else {
// if it were more than 50, then we'll give assertion error
// but we need to set the retries to 50
// so no more retires after this failure will occur
this.retries(0);
assert.ifError(Error("Retries were less than 50"));
}
}
assert.equal(a,b);
});
});
答案 1 :(得分:0)
retries()
做一些不是为此而设计的事情。
describe('Force fail after iterations', function () {
it('Math.random will be eventually give the same numbers', function () {
var tried = 0;
// When we have done 50 tries, we deem the function to be
// "strong" and consider it a success.
while (tried < 50) {
tried++;
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
if (a === b) {
throw new Error("function is weak");
}
}
});
});