【问题标题】:jasmine.js expect() does not work inside an asynchronous callbackjasmine.js expect() 在异步回调中不起作用
【发布时间】:2018-01-18 05:21:22
【问题描述】:

我结识了 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);
});

按预期失败。

但是,在回调中移动期望调用:

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);
  });
});

不知何故通过 :O

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

因此问题:

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

【问题讨论】:

  • 使用 Jasmine 2.0,您只需调用它("desc",function(done){... done(); ...})。只是说,因为这个线程在谷歌结果中很高:)

标签: javascript ajax bdd jasmine


【解决方案1】:

为 Jasmine 2 编辑

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

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

茉莉花 1

查看 Asynchronous Support 标题下 Jasmine site 上的 waitsFor()runs()

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

代码如下:

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);
    });
}

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

【讨论】:

  • 干杯!感觉很尴尬,就像我刚刚接受 RTFM 教育一样 :) 就像一个魅力 :)
  • 第一次运行结束时的“500”是什么()?
  • 第一次运行的 500 是异步操作的超时。或者,您可以在 api.sendGhostRequest 调用之后添加一个 setTimeout,以提供在超时时执行的替代情况。例如:setTimeout(function() { flag = true; }, 500);
  • 链接断开。搬到这里:jasmine.github.io/2.0/…
  • Jasmine 2 似乎已经删除了“waitsFor”,我完全不清楚应该使用什么来代替 :-(。可能值得用“Jasmine 1”和“Jasmine”来更新你的答案2" 部分,如果您知道新方法?
【解决方案2】:

正如@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);
});

【讨论】:

  • done 应该在函数参数中使用。 it("should be able to send a Ghost Request", function(done) {
  • 如何等待规范中的条件变为真?即,如果我想等待我的应用程序运行,但我没有“成功”回调,我该怎么办?我需要编写自己的“waitFor”轮询循环吗?
【解决方案3】:

查看runs()和waitfor()

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

runs 允许您等到 waitfor 完成。

async jasmine documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2012-07-03
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多