【问题标题】:Sinon fakeServer with mocha and axios带有 mocha 和 axios 的 Sinon fakeServer
【发布时间】:2017-05-02 17:54:21
【问题描述】:

我正在尝试让 sinon.fakeServer 让 axios 返回一个伪造的响应。我可以看到网络请求 404 或尝试转到实际 URL 时出现超时,而不是返回模拟的有效负载。

我的设置:

  describe('test call', () => {
    var server;
    beforeEach(() => {
      server = sinon.fakeServer.create();
      server.respondWith(
        "https://my.domain.com/myresource",
        [200, { "Content-Type": "application/json" }, "[]"]
      );
      server.autoRespond = true
    });
    it('returns empty array', done => {
      axios
        .get('https://my.domain.com/myresource')
        .then(res => {
          expect(true).to.equal(true);
          done()
        })
        .catch(err=>{
          console.log(err.message);
          expect(false).to.equal(true);
          done();
        });
    });
    afterEach(() => {
      server.restore();
    });
  })  

【问题讨论】:

    标签: mocha.js sinon axios


    【解决方案1】:

    您的执行环境似乎是 NodeJS,尽管没有提及。其他人也有同样的问题 - 看看here

    Sinon 团队还提到它是outside their scope,因为 XHR 应该在浏览器中正常工作,他们的假服务器在存根 XHR 对象时按预期工作。

    Axios 在服务器上运行时使用不同的库来发出请求,因此默认情况下此方案无法工作。 axios 有特定的模拟库,例如 moxios

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2017-11-21
      • 2016-10-22
      相关资源
      最近更新 更多