javascript jasmine.js expect() 在异步回调中不起作用

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

jasmine.js expect() does not work inside an asynchronous callback

javascriptajaxbddjasmine

提问by Val Redchenko

I'm getting acquainted with Jasmine (http://pivotal.github.com/jasmine/) and found something rather baffling:

我开始熟悉 Jasmine ( http://pivotal.github.com/jasmine/) 并发现一些令人困惑的事情:

it("should be able to send a Ghost Request", function() {
  var api = fm.api_wrapper;

  api.sendGhostRequest(function(response) {
    console.dir('server says: ', response);
  });

  expect(true).toEqual(false);
});

Fails as expected.

按预期失败。

However, moving the expect call inside the callback:

但是,在回调中移动 expect 调用:

it("should be able to send a Ghost Request", function() {
  var api = fm.api_wrapper;

  api.sendGhostRequest(function(response) {
    console.dir('server says: ', response);
    expect(true).toEqual(false);
  });
});

Somehow passes :O

不知何故通过:O

After some debugging: api.sendGhostRequest() does an asynchronous ajax request, and jasmine rushes past before the request has completed.

经过一番调试:api.sendGhostRequest()做了一个异步ajax请求,jasmine在请求完成之前就冲了过去。

Hence the question:

因此问题是:

How do I get jasmine to wait for ajax execution before ascertaining the test result?

如何让茉莉花在确定测试结果之前等待ajax执行?

采纳答案by halfs13

Edit for Jasmine 2

编辑茉莉花 2

Asynchronous tests have become much simpler in Jasmine 2. Any test that needs to handle asynchronous code can be written with a callback which will indicate the completion of the test. See the Jasmine 2 docsunder the header Asynchronous Support

Jasmine 2 中的异步测试变得更加简单。任何需要处理异步代码的测试都可以使用回调来编写,该回调将指示测试的完成。请参阅标题异步支持下的Jasmine 2 文档

it('should be able to send a ghost request', (done) => {
    api.sendGhostRequest((response) => {
        console.log(`Server says ${response}`);
        expect(true).toEqual(false);
        done();
    });
});

Jasmine 1

茉莉花1

Have a look at waitsFor()and runs()on the Jasmine siteunder the header Asynchronous Support.

看看waitsFor())运行(茉莉花网站的标题下的异步支持

Use of runs and waitsfor should force Jasmine to wait for the ajax call to finish or for some timeout.

使用 run 和 waitsfor 应该强制 Jasmine 等待 ajax 调用完成或超时。

The code would look like:

代码如下所示:

it("should be able to send a Ghost Request", function() {
    runs(function() {
        api.sendGhostRequest(function(response) {
            console.dir('server says: ', response);
            flag = true;
        });
    }, 500);

    waitsFor(function() {
        return flag;
    }, "Flag should be set", 750);

    runs(function() {
        expect(true).toEqual(false);
    });
}

In which case the expect would fail.

在这种情况下,期望会失败。

回答by ptim

As @pkopac commented, runs()and waitsFor()have been deprecated in v2 favour of using a done()callback as documented: https://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

作为@pkopac评论,runs()waitsFor()在使用的V2青睐已过时done()回调的记载:https://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

it("should be able to send a Ghost Request", function(done) {
    var api = fm.api_wrapper;

    var success = function(response) {
        console.dir('server says: ', response);
        expect(response).toEqual('test response')
        done();
    };

    api.sendGhostRequest(success);
});

回答by Ben McCormick

Look into runs() and waitfor()

查看 running() 和 waitfor()

Specifically you can call waitfor to check that the callback has run in some fashion (maybe using a boolean as a check?) and then run the expect afterwards.

具体来说,您可以调用 waitfor 来检查回调是否以某种方式运行(也许使用布尔值作为检查?),然后运行期望。

runs allows you to wait until the waitfor has completed.

运行允许您等到 waitfor 完成。

async jasmine documentation

异步茉莉花文档