使用nock在异步操作创建器中测试POST请求

时间:2017-12-22 19:16:49

标签: asynchronous testing redux fetch nock

我正在使用nock来模拟我的异步thunk动作创建者。对于GET请求,一切都按预期工作,但我还没有能够使POST请求正常工作。我已经阅读了我在网上找到的所有内容,但没有解决我的问题。我可以在失败的测试运行中看到响应主体。问题是网址没有匹配。有谁能发现问题?

测试文件:

describe('#saveReminder', () => {
    it(`creates ${constants.actions.REQUEST_SAVE_REMINDER}
      and ${constants.actions.RECEIVE_SAVE_REMINDER}`, () => {

      data = { payload: 'payload' };

      nock(constants.paths.API_AUTHORITY)
        .post('api/prospect/add-reminder', {
          prospect: 1,
          reminder_datetime: '2017-12-22T18:42:00.000Z',
          description: 'description',
        })
        .reply(200, { data } )

      const expectedActions = [
        { type: constants.actions.REQUEST_SAVE_REMINDER },
        { type: constants.actions.RECEIVE_SAVE_REMINDER, data }
      ]

      return store.dispatch(actions.saveReminder({
        id: 1,
        description: 'description',
        date: moment(),
        time: moment(),
      })).then(() => {
        expect(store.getActions()).toEqual(expectedActions)
      })
    })
  })

异步操作:

export function saveReminder({ id, description, date, time }) {
  return (dispatch) => {
    requestSaveReminder();
    return fetch(`${constants.paths.API_AUTHORITY}api/prospect/add-reminder`, {
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'X-Csrf-Token': Cookie.get('X-Csrf-Token'),
      },
      credentials: 'same-origin',
      method: 'POST',
      body: JSON.stringify(
        {
          prospect: id,
          reminder_datetime: moment(`${date.format('MM/DD/YYYY')} ${time.format('HH:mm')}`, 'MM/DD/YYYY HH:mm'),
          description,
        }
      ),
    }).then(response => {
      response.json();
    })
      .then(json => dispatch(receiveSaveReminder(json.data)))
      .catch(ex => dispatch(requestSaveReminderFailure(ex)));
  };
}

测试失败:

Object {
    -     "type": "REQUEST_SAVE_REMINDER",
    +     "data": [FetchError: request to http://localhost:8080/api/prospect/add-reminder failed, reason: Nock: No match for request {
    +   "method": "POST",
    +   "url": "http://localhost:8080/api/prospect/add-reminder",
    +   "headers": {
    +     "accept": [
    +       "application/json"
    +     ],
    +     "content-type": [
    +       "application/json"
    +     ],
    +     "accept-encoding": [
    +       "gzip,deflate"
    +     ],
    +     "user-agent": [
    +       "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
    +     ],
    +     "connection": [
    +       "close"
    +     ],
    +     "content-length": [
    +       89
    +     ]
    +   },
    +   "body": "{\"prospect\":1,\"reminder_datetime\":\"2017-12-22T18:56:00.000Z\",\"description\":\"description\"}"
    + }],
    +     "type": "REQUEST_SAVE_REMINDER_FAILURE",
        },
    -   Object {
    -     "data": Object {
    -       "payload": "payload",
    -     },
    -     "type": "RECEIVE_SAVE_REMINDER",
    -   },

网址似乎匹配。为什么会说Nock: No match for request?谢谢!

1 个答案:

答案 0 :(得分:0)

因为您不仅要匹配URL,还需要使用Nock验证预期的有效负载。如果您用.post('api/prospect/add-reminder', () => true)代替nock post()通话,我认为时间戳记的差异不会影响您。