javascript Jasmine 2.0:重构 1.3 的 running() 和 waitsFor()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20963038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:42:42  来源:igfitidea点击:

Jasmine 2.0: refactoring out 1.3's runs() and waitsFor()

javascriptjasmine

提问by thadk

The recently released Jasmine 2.0 removes the waits functionsand the runs()from the Async Jasmine 1.3.

最近发布的 Jasmine 2.0从 Async Jasmine 1.3 中删除了等待功能runs()

I have old 1.3 tests I'd like to transition to the new style.

我有旧的 1.3 测试,我想转换到新样式。

For the waits, in most cases it seems like you can write beforeEach()and afterEach()carefully for the same effect.

对于等待,在大多数情况下,您似乎可以仔细编写beforeEach()afterEach()获得相同的效果。

What is the best way to reproduce the runs()which simply executes the contained functions sequentially?

重现runs()简单地按顺序执行包含的函数的最佳方法是什么?

My first try:

我的第一次尝试:

runs(function() {
  expect(true).toBe(true);
}

becomes

变成

(function() {
  expect(true).toBe(true);
})()

回答by Tony

It is possible to use a setTimeout in your it() block.

可以在 it() 块中使用 setTimeout。

it("is asynchronous", function(done) {
  var isItDone = false;
  $.ajax('/some/url').success(function() { isItDone = true; });

  setTimeout(function(){
    expect(isItDone).toBeTrue();
    done(); // call this to finish off the it block
  }, 500);

});

However, I found that that slowed down my test suite dramatically so I created my own extension which recreates the polling functionality that waitsFor provided.

但是,我发现这显着降低了我的测试套件的速度,因此我创建了自己的扩展,它重新创建了 waitsFor 提供的轮询功能。

https://gist.github.com/abreckner/110e28897d42126a3bb9

https://gist.github.com/abreckner/110e28897d42126a3bb9

回答by Gregg

In jasmine 1.3 and previous, the runsand waits/waitsForshould have only been necessary if you had some asynchronous code that you needed to wait until it was done before doing the next part of the test. In that case you would have something like:

在 jasmine 1.3 及更早版本中,只有当您有一些异步代码需要等待它完成后再进行下一部分测试时,才需要runsand waits/ waitsFor。在这种情况下,你会有类似的东西:

it("is asynchronous", function() {
    var isItDone = false;
    runs(function() {
        $.ajax('/some/url').success(function() { isItDone = true; });
    });

    waitsFor(function() {
        return isItDone;
    });

    runs(function() {
        // this won't run until the waitsFor returns true
    });
});

Jasmine 2.0 moved to using a donecallback for beforeEach, it, and afterEachif they do something asynchronous that you need to wait for.

茉莉花2.0移到使用done回调beforeEachit以及afterEach如果他们做一些事情异步,你需要等待。

beforeEach(function(done) {
    $.ajax('/some/url').success(done);
});

it("is asynchronous", function() {
    // this won't run until the done callback is invoked from the beforeEach
});