Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing ReactJs-使用expectSaga测试redux saga中的多个调用_Unit Testing_Reactjs_Ecmascript 6_Redux_Redux Saga - Fatal编程技术网

Unit testing ReactJs-使用expectSaga测试redux saga中的多个调用

Unit testing ReactJs-使用expectSaga测试redux saga中的多个调用,unit-testing,reactjs,ecmascript-6,redux,redux-saga,Unit Testing,Reactjs,Ecmascript 6,Redux,Redux Saga,我正在使用expectSaga(“”)测试我的一个saga,我想知道如何测试同一个saga中的多个调用 Sagas.js export function* fetchSomething(arg){ const response = yield call(executeFetch, arg); if(response.status === 200){ // trigger success action } else if (response.status >= 400){

我正在使用expectSaga(“”)测试我的一个saga,我想知道如何测试同一个saga中的多个调用

Sagas.js

export function* fetchSomething(arg){
  const response = yield call(executeFetch, arg);
  if(response.status === 200){
    // trigger success action
  } else if (response.status >= 400){
    const errResp = yield response.json();
    const errorCode = yield call(sharedUtilToExtractErrors, errResp);
    yield put(
      { type: 'FETCH_FAILED', errorMessage: UI_ERR_MSG, errorCode }
    );
  }
}
单元测试

import { expectSaga } from 'redux-saga-test-plan';

describe('fetchSomething', () => {

   // positive paths

   // ..

   // negative paths

   it('fetches something and with status code 400 triggers FETCH_FAILED with error message and extracted error code', () => {
     const serverError = { message: 'BANG BANG BABY!' };
     const koResponse = new Response(
       JSON.stringify(serverError),
       { status: 400, headers: { 'Content-type': 'application/json' } }
     );

     return expectSaga(fetchSomething)
        .provide(
          {
            call: () => koResponse,
            call: () => serverError.message,
          }
        )
        .put({
           type: 'FETCH_FAILED', errorMessage: UI_ERR_MSG, serverError.message
        })
        .run();
    })
})
显然,在同一个对象中,两次将“call”属性传递给provide()是行不通的,但是两次调用provide()也行不通。有什么建议吗

谢谢

这是您根据以下条件提供多个通话的方式:

或者,如果您很懒,不想指定参数:

import * as matchers from 'redux-saga-test-plan/matchers';

.provide(
 [matchers.call.fn(executeFetch), koResponse],
 [matchers.call.fn(sharedUtilToExtractErrrors), serverError.message],
)
这两种方法对我都不起作用,尽管出于某种原因,它没有模仿依赖项,仍然调用它们会导致错误

我使用动态提供程序解决了以下问题:

.provide({
  // select(effect, next) { return 'something-for-a-selector' },
  call(effect) {
    switch(effect.fn.constructor.name) {
      case executeFetch.constructor.name: return koResponse;
      case sharedUtilToExtractErrors.constructor.name: return serverError.message;
      default: throw new Error('Unknown function called in test');
    }
  }
})
.provide({
  // select(effect, next) { return 'something-for-a-selector' },
  call(effect) {
    switch(effect.fn.constructor.name) {
      case executeFetch.constructor.name: return koResponse;
      case sharedUtilToExtractErrors.constructor.name: return serverError.message;
      default: throw new Error('Unknown function called in test');
    }
  }
})