【问题标题】:Using nock to test POST request in async action creator使用 nock 在异步操作创建器中测试 POST 请求
【发布时间】:2018-06-05 09:25:11
【问题描述】:

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

测试文件:

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?谢谢!

【问题讨论】:

    标签: asynchronous testing redux fetch nock


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 2021-02-24
      • 1970-01-01
      • 2017-06-13
      • 2019-03-07
      • 2015-02-26
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多