Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
Javascript 将对象传递给函数以进行API调用_Javascript_Axios_Web Api Testing - Fatal编程技术网

Javascript 将对象传递给函数以进行API调用

Javascript 将对象传递给函数以进行API调用,javascript,axios,web-api-testing,Javascript,Axios,Web Api Testing,我正在为我正在进行的一个项目创建一个API测试框架,我正在尝试设置一个测试,并创建一个框架,确保它是干的 我有一个JSON对象,我想传递给我的API调用函数。我希望能够将不同的JSON对象传递给这个函数 因此,每个测试都会有一个不同的JSON对象,但我可以为实际的API调用调用相同的函数 我正在使用axios发布到端点。我试过用谷歌搜索如何做到这一点,但我不确定我是否遗漏了一些简单的东西,或者是完全错了。我尝试过使用JSON.stringify/parse和其他方法传递对象,但在axios使用时

我正在为我正在进行的一个项目创建一个API测试框架,我正在尝试设置一个测试,并创建一个框架,确保它是干的

我有一个JSON对象,我想传递给我的API调用函数。我希望能够将不同的JSON对象传递给这个函数

因此,每个测试都会有一个不同的JSON对象,但我可以为实际的API调用调用相同的函数

我正在使用axios发布到端点。我试过用谷歌搜索如何做到这一点,但我不确定我是否遗漏了一些简单的东西,或者是完全错了。我尝试过使用JSON.stringify/parse和其他方法传递对象,但在axios使用时失败

以下是测试函数:

describe('Reminder', () => {
  it('It should create a reminder', async () => {
    const reminder = {
      title: 'Test Title',
      description: 'Test Description',
      host: 'User One',
      eventLocation: 'Home',
      eventDate: '01-01-2019'
    };

    const response = await reminderService.createReminder(reminder);
    expect(response.status).to.equal(201);
  });
async function createReminder(reminderObject) {
  console.log('this is the obj' + reminderObject);
  const response = await axios.post(`${config.get('baseUrl')}/reminder`, {
    reminderObject
  });

  return response;
}

module.exports.createReminder = createReminder;
我正在尝试将提醒对象传递给CreateRemembers函数

下面是这个函数:

describe('Reminder', () => {
  it('It should create a reminder', async () => {
    const reminder = {
      title: 'Test Title',
      description: 'Test Description',
      host: 'User One',
      eventLocation: 'Home',
      eventDate: '01-01-2019'
    };

    const response = await reminderService.createReminder(reminder);
    expect(response.status).to.equal(201);
  });
async function createReminder(reminderObject) {
  console.log('this is the obj' + reminderObject);
  const response = await axios.post(`${config.get('baseUrl')}/reminder`, {
    reminderObject
  });

  return response;
}

module.exports.createReminder = createReminder;
目前我在调用端点时得到了404。当我console.log()提醒对象时,它作为[object object]出现


传递缺少字段的提醒对象以测试API的验证的目的。

您实际上是在另一个对象中发送该对象。如果您尝试以下方法:

const response = await axios.post(`${config.get('baseUrl')}/reminder`, {
    ...reminderObject
});

您实际上是在另一个对象中发送对象。如果您尝试以下方法:

const response = await axios.post(`${config.get('baseUrl')}/reminder`, {
    ...reminderObject
});

非常感谢。这就成功了。你能更详细地解释一下这个问题以及…对象的含义吗?或者发布一个链接,这样我就可以读到它了。。。称为扩展运算符。最好是查找更多细节,你可以用谷歌搜索。它在不同的情况下非常有用。在这种情况下,您将从提醒对象获取道具,并将其放入通过axios发送的对象中。您可以跳过新对象,只需放置提醒对象即可通过axios发送相同的参数。谢谢。这就成功了。你能更详细地解释一下这个问题以及…对象的含义吗?或者发布一个链接,这样我就可以读到它了。。。称为扩展运算符。最好是查找更多细节,你可以用谷歌搜索。它在不同的情况下非常有用。在这种情况下,您将从提醒对象获取道具,并将其放入通过axios发送的对象中。您可以跳过新对象,只需放置提醒对象即可通过axios发送相同的参数。