将期望未满足纳入redux-saga-test计划

时间:2019-01-03 08:23:18

标签: reactjs unit-testing redux-saga redux-saga-test-plan

所以我有个传奇故事,显示要提取一些要显示在表中的数据。

动作创建者如下

export const fetchInstanceDataSetAssocSuccess = (records) => {
  return {
    type: actionTypes.FETCH_INSTANCE_DATASETS_ASSOC_SUCCESS,
    records: records
  }
 }

export const fetchInstanceDataSetAssocFailed = (error) => {
  return {
    type: actionTypes.FETCH_INSTANCE_DATASETS_ASSOC_FAILED,
    error: error
  }
}

export const fetchInstanceDataSetAssocStart = () => {
  return {
   type: actionTypes.FETCH_INSTANCE_DATASETS_ASSOC_START
  }
}

export const fetchInstanceDataSetAssoc = () => {
   return {
     type: actionTypes.FETCH_INSTANCE_DATASETS_ASSOC_INITIATE
   }
}

我的传奇如下

    function * fetchInstanceDataSetAssocSaga (action) {
      yield put(instanceDataSetAssocActions.fetchInstanceDataSetAssocStart())
      const useMockData = yield constants.USE_MOCK_DATA
      if (useMockData) {
        yield delay(constants.MOCK_DELAY_SECONDS * 1000)
      }
      try {
        const res = (useMockData)
         ? (yield constants.INSTANCE_DATASET_ASSOC)
         : (yield call(request, {url: 
         API_URLS.INSTANCE_DATASET_ASSOC_API_ENDPOINT, method: 'GET'}))

    yield put(instanceDataSetAssocActions.fetchInstanceDataSetAssocSuccess(res.data))
    } catch (error) {
       yield 
put(instanceDataSetAssocActions.fetchInstanceDataSetAssocFailed(error))
  }
}

监视Saga的动作如下

export function * watchInstanceDataSetAssocSaga () {
  yield takeEvery(actionTypes.FETCH_INSTANCE_DATASETS_ASSOC_INITIATE, 
  fetchInstanceDataSetAssocSaga)
}

测试用例如下

  describe('load instance dataset assoc table', () => {
  test('update state with  instance-dataset records for landing page',() => {
    const finalState = {
      records: constants.INSTANCE_DATASET_ASSOC.data,
      loading: false,
      error: false
    }
    const requestParam = {url: API_URLS.INSTANCE_DATASET_ASSOC_API_ENDPOINT, method: 'GET'}
    return expectSaga(watchInstanceDataSetAssocSaga)
      .provide([[call(request,requestParam),constants.INSTANCE_DATASET_ASSOC]])
      .withReducer(instanceDataSetAssoc)
      .put(instanceDataSetAssocActions.fetchInstanceDataSetAssocStart())
      .put(instanceDataSetAssocActions.fetchInstanceDataSetAssocSuccess(constants.INSTANCE_DATASET_ASSOC.data))
      .dispatch(instanceDataSetAssocActions.fetchInstanceDataSetAssoc())
      .hasFinalState(finalState)
      .silentRun()
  })
})

我为此收到以下错误。

 SagaTestError:
put expectation unmet:
at new SagaTestError (node_modules/redux-saga-test-plan/lib/shared/SagaTestError.js:17:57)
  at node_modules/redux-saga-test-plan/lib/expectSaga/expectations.js:63:13
  at node_modules/redux-saga-test-plan/lib/expectSaga/index.js:572:7
      at Array.forEach (<anonymous>)
  at checkExpectations (node_modules/redux-saga-test-plan/lib/expectSaga/index.js:571:18)

我正确地遵循了文档,但是仍然出现上述错误。

1 个答案:

答案 0 :(得分:0)

也许已经晚了,但我找到了答案,也许会对您有所帮助 由于库超时,可能会发生此错误,请尝试使用.run(false)关闭超时 原始链接https://github.com/jfairbank/redux-saga-test-plan/issues/54