简体   繁体   English

如何使用 sinon 模块模拟 axios 请求

[英]How to mock an axios request using sinon modules

There seems to be so many different ways to do this, but I am trying to use just sinon, sinon-test, chai/mocha, axios, httpmock modules.似乎有很多不同的方法可以做到这一点,但我试图只使用 sinon、sinon-test、chai/mocha、axios、httpmock 模块。 I am not able to successfully mock a GET call made using axios.我无法成功模拟使用 axios 进行的 GET 调用。 I want to be able to mock the response from that axios call so the unit test won't actually have to make the external API request.我希望能够模拟来自该 axios 调用的响应,因此单元测试实际上不必发出外部 API 请求。

I've tried setting up a basic unit test by creating a sandbox, and using sinon stub to set up a GET call and specify the expected response.我尝试通过创建沙箱来设置基本单元测试,并使用 sinon 存根设置 GET 调用并指定预期响应。 I'm unfamiliar with JavaScript and NodeJS.我不熟悉 JavaScript 和 NodeJS。

// Main class (filename: info.js)

function GetInfo(req, res) {
    axios.get(<url>).then(z => res.send(z.data));
}

// Test class (filename: info.test.js)

it ("should return info", () => {
    const expectedResponse = "hello!";
    const res = sinon.spy();
    const aStub = sinon.stub(axios, "get").resolves(Promise.resolve(expectedResponse));

    const req = httpMock.createRequest({method:"get", url:"/GetInfo"});

    info.GetInfo(req, res);

    // At this point, I need to evaluate the response received (which should be expectedResponse)
    assert(res.data, expectedResponse); // data is undefined, res.status is also undefined

    // How do I read the response received?

});

I need to know how to read the response that is supposed to be sent back (if it is being captured in the first place by sinon).我需要知道如何读取应该发回的响应(如果它首先被 sinon 捕获)。

I'm assuming the response you're wanting to check is the z.data being passed to res.send(z.data)我假设您要检查的z.data是传递给res.send(z.data)

I don't think your Sinon Spy is being set up correctly.我认为您的 Sinon Spy 设置不正确。

In your example, res is a function created by sinon.在您的示例中, res是由 sinon 创建的函数。 This function won't have a property data .这个函数没有属性data

You probably want to create a spy like this:你可能想创建一个这样的间谍:

const res = {
  send: sinon.spy()
}

This gives you a res object which has a spy with the key send .这为您提供了一个res对象,其中有一个带有密钥send的间谍。 You can then make assertions about the parameters used to call res.send然后,您可以对用于调用res.send的参数进行断言

it ("should return info", () => {
    const expectedResponse = "hello!";
    const res = {
      send: sinon.spy()
    };
    const aStub = sinon.stub(axios, "get").resolves(Promise.resolve(expectedResponse));

    const req = httpMock.createRequest({method:"get", url:"/GetInfo"});

    info.GetInfo(req, res);

    // At this point, I need to evaluate the response received (which should be expectedResponse)
    assert(res.send.calledWith(expectedResponse)); // data is undefined, res.status is also undefined

});

Dont know if this helps but you may not be getting the correct response because resolves is a return with a promise wrap over it.不知道这是否有帮助,但您可能没有得到正确的响应,因为 resolves 是一个带有 Promise 包装的返回。

So by using resolves and inside it a Promise.resolve you are actually returning Promise wrap in a Promise.因此,通过使用 resolves 并在其中使用 Promise.resolve,您实际上是在 Promise 中返回 Promise 包装。

Maybe you can try changing the code to the one below.也许您可以尝试将代码更改为下面的代码。

const aStub = sinon.stub(axios, "get").resolves(Promise.resolve(expectedResponse));

to

const aStub = sinon.stub(axios, "get").resolves(expectedResponse);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM