【问题标题】:How to pass this Jasmine test 'should recover on fetch error'如何通过此 Jasmine 测试“应在获取错误时恢复”
【发布时间】:2021-11-21 01:25:56
【问题描述】:

我正在学习茉莉花。我无法通过考试。

下面的 it() 在 Records 的描述中,因此 Records 应该在获取错误时恢复。对于这篇文章,我将预期的 JSON 对象替换为“此处的对象”。这不是问题及其敏感数据。

我目前遇到“预期的间谍日志已被调用”失败。我认为这与运行 console.log 有关吗?但是当我插入一个console.log时,我得到了失败的'Expected spy log to not be called。所以这让我很困惑。我当然可能完全看错了。

我的代码只是一个获取,从一个名为 retrieve() 的函数返回数据。该函数正在导出,并在此处导入以用于测试。

it('should recover on fetch error', function(done){
window.path = "http://localhost:3000/recordszzz";
spyOn(console, "log").and.callFake(function(){});

function check(output) {
  window.path = "http://localhost:3000/records";
  expect(console.log).toHaveBeenCalled();
  console.log.calls.reset();
  retrieve().then(function(output){
    var expected = {Object here};
    expect(output).toEqual(expected);
    expect(console.log).not.toHaveBeenCalled();
    done();
  });
}

retrieve().then(check);

【问题讨论】:

    标签: javascript unit-testing jasmine


    【解决方案1】:

    我发现了我的问题,结果发现预期的结果永远不会出现。这是代码中的一个非常具体的问题。在 .catch 上,我执行 console.log 错误,..然后使用更正的 URL 请求重新尝试获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 2017-08-15
      • 2013-08-13
      • 2020-12-14
      • 1970-01-01
      • 2020-03-05
      • 2012-08-19
      相关资源
      最近更新 更多