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

Passing an object to a function to make an API call

我正在为我正在处理的项目创建一个 API 测试框架,我正在尝试设置测试并创建框架以确保它是 DRY。

我有一个 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() reminderObject 时,它作为 [object object].

目的是传递一个缺少字段的提醒对象来测试 API 的有效性。

您实际上是在另一个对象中发送对象。如果您尝试这样做:

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