Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何验证是否使用axios模拟适配器发出了请求?_Unit Testing_Axios_Axios Mock Adapter - Fatal编程技术网

Unit testing 如何验证是否使用axios模拟适配器发出了请求?

Unit testing 如何验证是否使用axios模拟适配器发出了请求?,unit-testing,axios,axios-mock-adapter,Unit Testing,Axios,Axios Mock Adapter,我正在使用 我想知道如何验证被测系统是否实际调用了端点 在本例中: var axios = require('axios'); var MockAdapter = require('axios-mock-adapter'); // This sets the mock adapter on the default instance var mock = new MockAdapter(axios); // Mock any GET request to /users // arguments

我正在使用

我想知道如何验证被测系统是否实际调用了端点

在本例中:

var axios = require('axios');
var MockAdapter = require('axios-mock-adapter');

// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
  users: [
    { id: 1, name: 'John Smith' }
  ]
});
我如何判断是否调用了get-on'/users'

我正在寻找类似于你在开玩笑时所能做的事情:

expect(mockFunc).toHaveBeenCalledTimes(1)

我意识到,在使用函数进行回复时,我可以使用一些自定义逻辑,并设置一个局部变量,指示是否已发出请求。我只是想知道是否有一种更干净的方法来做这件事。

注意:这个答案现在已经过时了,取而代之的是Laszlo Sarvold。


axios模拟适配器似乎没有内置此功能,但如果您使用的是jest,则可以使用jest.spyOn

对于你上面的例子

let spy = jest.spyOn(axios, "get");
//run http request here
expect(spy).toHaveBeenCalled();

注意:根据您使用的内容,您可能需要将expect语句包装在setTimeout(函数,0)中,以便它正常工作

//OP's Code
var axios = require('axios');
var MockAdapter = require('axios-mock-adapter');

// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
  users: [
    { id: 1, name: 'John Smith' }
  ]
});

const spy = jest.spyOn(mock, 'onGet');

//function call here

expect(spy).toHaveBeenCalledWith('/users')
实际上,由于模拟“拦截”axios调用,您无法测试axios.get调用。。因为它从未真正发生过。但是mock.onGet函数确实会被执行,您也可以验证使用了什么路由来执行它

当你嘲笑axios时,试图监视它会使你出现如下错误

expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "/users"

Number of calls: 0
干杯。

根据,有一个现成的功能用于验证请求呼叫:

expect(mock.history.post.length).toBe(1); // times called
expect(mock.history.post[0].data).toBe(JSON.stringify({ foo: "bar" })); // posted object

有没有办法监视特定的URL?我在一个函数中有多个get,我想验证每个get。可以将toHaveBeenCalled更改为toHaveBeenCalledWith。例如:expect(spy).tohavebeincalledwith('/users');是的,我就是这么做的。请注意,如果您执行的是post而不是get,那么还将具有payload参数。在这种情况下,如果您不关心验证发送的有效负载数据,那么您可以期望(spy).toHaveBeenCalledWith('/users',expect.anything());只是为了验证地址是否有效,但当我运行第二个测试时,toHaveCalledTimes返回2,不知何故它并没有清除旧的调用。@Sarmadsha请在每次测试后尝试运行jest.clearAllMocks()。这对我不起作用。还有其他人有此问题吗?自述文件的底部有正确答案中提到的部分: