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

时间:2019-08-30 21:28:43

标签: javascript axios web-api-testing

我正在为正在处理的项目创建API测试框架,并且试图建立测试并创建框架以确保其干燥。

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

因此,每个测试将具有不同的JSON对象,但我将能够为实际的API调用调用相同的函数。

我正在使用axios POST到端点。我已尝试使用谷歌搜索方法来执行此操作,但不确定是否缺少一些简单的方法或完全错误的方法。我尝试使用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);
  });

我正在尝试将提醒对象传递给createReminder函数。

这是该函数:

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验证的目的。

1 个答案:

答案 0 :(得分:0)

您实际上是在另一个对象中发送对象。如果您改用此方法:

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