如何多次调用assert直到它成立? [英] How to call assert for several time until it's true?
问题描述
说我得到如下一些基本断言:
Say that I got some basic assert as below:
expect(myObject.getValue())。to.equal(5);
从 myObject.getValue()
返回的值应为 5
在其他地方运行了几个代码之后,所以我只需要让此值
进行更新。
And the returned value from myObject.getValue()
should be 5
after several code running over some other place, so I just need to let this value
to be updated.
我的问题是,创建这种测试的代码是什么?
My question is, what is the code for creating this kind of test?
推荐答案
Mocha为重试。您可以在测试中使用 this.retries(number_of_tries)
。像这样:
Mocha has a facility for retrying tests. You can just use this.retries(number_of_tries)
in your test. Like this:
it("something", function () {
this.retries(10);
expect(myObject.getValue()).to.equal(5);
});
您也可以在描述
中使用它如果要设置整个测试的重试次数,则阻止。请注意,如果使用 this.retries
,则它不会出现在箭头函数中(()=> ...
),因为箭头函数使 this
保持其在箭头函数出现的范围内的值。您必须使用完整功能(功能()...
)。
You can also use it in a describe
block if you want to set the number of retries for an entire set of tests. Note that if you use this.retries
, it cannot appear in an arrow function (() => ...
) because arrow functions let this
remain the value it had in the scope in which the arrow function appeared. You must use a full function (function () ...
).
这篇关于如何多次调用assert直到它成立?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!