Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
Reactjs 测试具有api调用的redux saga函数_Reactjs_Redux_Redux Saga - Fatal编程技术网

Reactjs 测试具有api调用的redux saga函数

Reactjs 测试具有api调用的redux saga函数,reactjs,redux,redux-saga,Reactjs,Redux,Redux Saga,下面是一个简单的api调用 api import axios from 'axios'; export default { json: { getData: () => axios.get('https://jsonplaceholder.typicode.com/todos').then(res => res.data.slice(0, 5)), }, }; 此api调用正在redux saga函数中使用 重演传奇 import { put, fork,

下面是一个简单的api调用

api

import axios from 'axios';   
export default {
  json: {
    getData: () => axios.get('https://jsonplaceholder.typicode.com/todos').then(res => res.data.slice(0, 5)),
  },
};
此api调用正在redux saga函数中使用

重演传奇

import {
  put, fork, takeLatest, call,
} from 'redux-saga/effects';
import { GET_DATA_SAGA } from '../actions/types';
import api from '../api';
import { fetchDataSuccess, fetchDataError } from '../actions/dataActions';

export function* getData() {
  try {
    const data = yield call(api.json.getData);

    console.log(data);

    yield put(fetchDataSuccess(data));
  } catch (err) {
    yield put(fetchDataError(err));
  }
}

export function* watchData() {
  yield takeLatest(GET_DATA_SAGA, getData);
}

export default function* () {
  yield fork(watchData);
}
我怎样才能测试这个函数。我看过一些指南,但我没有偶然发现一个不复杂的单元测试

这是到目前为止我的单元测试,这是我取得的成绩

重演传奇单元测试

import {put, fork, takeLatest, call} from 'redux-saga/effects';
import axios from 'axios';
import { GET_DATA_SAGA } from '../actions/types';
import {expectSaga} from 'redux-saga-test-plan';
import { fetchDataSuccess, fetchDataError } from '../actions/dataActions';
import api from '../api';

import {getData} from './data';

it('testing api call', () => {

    return expectSaga(api.json.getData)
})

你可以这么简单,如果你有任何参数要传递给你的生成器函数,你可以。 要转到下一个声明,只需继续编写
generator.next()
。 对于您来说,下一步可能是操作
fetchDataSuccess

import{put,fork,takeLatest,call}来自'redux saga/effects';
从“axios”导入axios;
从“../actions/types”导入{GET_DATA_SAGA};
从“redux saga测试计划”导入{expectSaga};
从“../actions/dataActions”导入{fetchDataSuccess,fetchDataError};
从“../api”导入api;
从“./data”导入{getData};
描述('我的功能',()=>{
它('测试api调用',()=>{
const generator=getData();
expect(generator.next()).to.equal(yourApiResponse);
})

})
console.log(生成器)
正在给我一个类型及其未定义的数据。我是否应该像这样传递api调用
fetchDataSucces(api.json.getData)
?我更新了代码。生成器必须是生成器函数
getData()