【问题标题】:Sinon Fake server not auto respondingSinon Fake 服务器没有自动响应
【发布时间】:2012-09-26 12:19:24
【问题描述】:

您好,我正在使用 fetch 测试一个集合,当我调用它时,只有在调用 server.response 我得到所需的结果后,假服务器才得到响应。

为什么会这样?

我的代码

  beforeEach( function() {
    server = sinon.fakeServer.create();
    server.autoRespond = true;
    colhedoraList = new ColhedoraList();
  });

.
.
.
    var spy       = sinon.spy(colhedoraList, 'parse');

    server.respondWith("GET", "getColhedoraInfo",
      [200, {"Content-Type": "application/json"},
      '[{"id":"1","talhaoAtual":1,"posicionamentos":[{"lat":-23.9317401,"lng":-50.2210379,"elevadorLigado":true,"horario":"2012-09-21T11:27:58Z"},{"lat":-23.931544,"lng":-50.2161884,"elevadorLigado":true,"horario":"2012-09-21T11:28:02Z"}]}]']);

    colhedoraList.fetch({add: true});
    server.respond();
    expect(spy).toHaveBeenCalled();
    expect(spygmaps).toHaveBeenCalledTwice();
    expect(colhedoraList.get(1).get('talhaoAtual')).toEqual(1);    <<< ALL EXPECTS FAIL, If I don't call respond().

【问题讨论】:

    标签: javascript sinon


    【解决方案1】:

    如果您在规范中的任何位置使用模拟计时器 (sinon.useFakeTimers),这可能会阻止自动回复器工作。 addRequest 方法创建一个超时,在该超时内实际响应请求。默认等待时间为 10 毫秒。

    【讨论】:

    • 不,不是,我是通过阅读诗乃的实际源码才发现的。你可以在他们的 GitHub 上提交一个问题,要求他们更新他们的文档。
    猜你喜欢
    • 2016-02-19
    • 2014-06-04
    • 2011-08-22
    • 2017-01-09
    • 2017-01-04
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多