javascript 茉莉花 run 和 waitsFor 实际上是做什么的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16400047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do jasmine runs and waitsFor actually do?
提问by Frederick Roth
I use jasmine runs and wait to test asynchronous operations. Everything works fine but I'm not quite sure what goes on behind the scenes.
我使用 jasmine 运行并等待测试异步操作。一切正常,但我不太确定幕后发生了什么。
The jasmine documentation states the following example to which I added three log statement.
茉莉花文档说明了我添加了三个日志语句的以下示例。
describe("Asynchronous specs", function() {
var value, flag;
it("should support async execution of test preparation and exepectations", function() {
runs(function() {
flag = false;
value = 0;
setTimeout(function() {
flag = true;
}, 500);
});
waitsFor(function() {
value++;
if(flag) {
console.log("A");
}
return flag;
}, "The Value should be incremented", 750);
console.log("B");
runs(function() {
console.log("C");
expect(value).toBeGreaterThan(0);
});
});
});
});
});
The first runs
and waitsFor
are perfectly clear to me. Runs
starts an asynchronous operation and waitsFor
waits for a condition.
第一runs
和waitsFor
是完全清晰。Runs
启动异步操作并waitsFor
等待条件。
However I do not understand why the second runs
does not start until the waitsFor
is finished. The waitsFor
is not a blocking call.
但是我不明白为什么第二个runs
直到waitsFor
完成才开始。这waitsFor
不是阻塞调用。
My guess is that waitsFor
implicitly blocks any following runs
call until it is finished. Is this so?
我的猜测是waitsFor
隐式阻止任何后续runs
调用,直到它完成。是这样吗?
My evidence is that the console.log statements output:
我的证据是 console.log 语句输出:
B A C
BAC
But if waitsFor
would really block it should be
但如果waitsFor
真的会阻止它应该是
A B C
美国广播公司
采纳答案by Sam
waitsFor
does block until the conditions it's waiting for are met or it times out.
waitsFor
确实会阻塞,直到满足它等待的条件或超时。
From the jasmine docs: "waitsFor() provides a better interface for pausing your spec until some other work has completed. Jasmine will wait until the provided function returns true before continuing with the next block.".
来自jasmine 文档:“waitsFor() 提供了一个更好的界面来暂停你的规范,直到其他一些工作完成。在继续下一个块之前,Jasmine 将等待所提供的函数返回 true。”。
The linked docs also have a slightly clearer example or waitsFor
.
链接的文档也有一个稍微清晰的示例或waitsFor
.
EDIT: Ah I see what you mean now. waitsFor
won't block JS that isn't wrapped in runs
, waitsFor
, ect.
编辑:啊,我明白你的意思了。waitsFor
不会阻止未包含在runs
, 等中的JS waitsFor
。
What jasmine is doing is taking the function passed to it via runs
or waitsFor
and if jasmine is not currently waiting, it executes the function immediately. If it is waiting, it doesn't call it until it's finished waiting.
jasmine 正在做的是通过runs
or获取传递给它的函数waitsFor
,如果 jasmine 当前没有等待,它会立即执行该函数。如果它正在等待,它不会在等待完成之前调用它。
That doesn't stop the console.log
as it's been passed to jasmine so jasmine can't prevent it from being executed straight away.
这并没有停止,console.log
因为它已传递给 jasmine,因此 jasmine 无法阻止它立即执行。
回答by Frederick Roth
The solution is in the documentation:
解决方案在文档中:
Multiple runs() blocks in a spec will run serially. (Jasmine Documentation)
规范中的多个 running() 块将串行运行。(茉莉花文档)
回答by George
From the site: http://www.htmlgoodies.com/beyond/javascript/test-asynchronous-methods-using-the-jasmine-runs-and-waitfor-methods.html#fbid=mzNDUVfhFXg
来自网站:http: //www.htmlgoodies.com/beyond/javascript/test-asynchronous-methods-using-the-jasmine-runs-and-waitfor-methods.html#fbid=mzNDUVfhFXg
Jasmine will call the runs() and waitsFor() methods in the order you passed them. As soon as the JS parser gets to a waitsFor() method it will poll it until it returns true and only then will it continue onto the next runs() method.
Jasmine 将按照您传递它们的顺序调用 running() 和 waitsFor() 方法。一旦 JS 解析器到达一个 waitsFor() 方法,它就会轮询它直到它返回 true,然后它才会继续下一个 running() 方法。
Essentially, the runs() and waitsFor() functions stuff an array with their provided functions. The array is then processed by jamine wherein the functions are invoked sequentially. Those functions registered by runs() are expected to perform actual work while those registered by waitsFor() are expected to be 'latch' functions and will be polled (invoked) every 10ms until they return true or the optional registered timeout period expires. If the timeout period expires an error is reported using the optional registered error message; otherwise, the process continues with the next function in the array. Presumably, expects within the runs() can also trigger a failure report (and perhaps even in the latch functions themselves).
本质上,runs() 和waitsFor() 函数用它们提供的函数填充一个数组。然后由 jamine 处理数组,其中函数被顺序调用。那些由runs() 注册的函数应该执行实际工作,而那些由waitsFor() 注册的函数应该是“闩锁”函数,并且每10 毫秒会轮询(调用)一次,直到它们返回true 或可选的注册超时期限到期。如果超时期限到期,则使用可选的注册错误消息报告错误;否则,该过程将继续使用数组中的下一个函数。据推测,runs() 中的 expects 也可以触发故障报告(甚至可能在闩锁函数本身中)。
The documentation is particularly obtuse on these asynchronous features.
文档对这些异步功能的描述特别迟钝。