Javascript 在if语句中测试redux saga并使用实际值
如何在if语句或try/catch中测试函数?比如说,Javascript 在if语句中测试redux saga并使用实际值,javascript,testing,redux-saga,Javascript,Testing,Redux Saga,如何在if语句或try/catch中测试函数?比如说, export function* onFetchMessages(channel) { yield put(requestMessages()) const channel_name = channel.payload try { const response = yield call(fetch,'/api/messages/'+channel_name) if(response.
export function* onFetchMessages(channel) {
yield put(requestMessages())
const channel_name = channel.payload
try {
const response = yield call(fetch,'/api/messages/'+channel_name)
if(response.ok){
const res = yield response.json();
const date = moment().format('lll');
yield put(receiveMessages(res,channel.payload,date))
}
} catch (error){
yield put(rejectMessages(error))
}
}
我需要输入一个实际存在于数据库中的通道名,以便它返回一个有效的响应来执行后续的输出,否则它将抛出一个错误。此外,我将收到一条错误消息,无法读取未定义的json属性,因此由于此错误消息,无法达到该属性之后的收益率。
所以我的第一个问题是“ifresponse.ok”,但即使我删除它,yield response.json也会返回一个错误,而且之后的yield将不会执行。
如果有人能告诉我如何测试这些,我将不胜感激。将响应对象传递给上一次执行并测试条件,我会这样做,希望这有帮助:
export function* onFetchMessages(channel) {
try {
yield put(requestMessages())
const channel_name = channel.payload
const response = yield call(fetch,'/api/messages/'+channel_name)
if(response.ok){
const res = yield response.json();
const date = moment().format('lll');
yield put(receiveMessages(res,channel.payload,date))
}
} catch (error){
yield put(rejectMessages(error))
}
}
describe('onFetchMessages Saga', () => {
let output = null;
const saga = onFetchMessages(channel); //mock channel somewhere...
it('should put request messages', () => {
output = saga.next().value;
let expected = put(requestMessages()); //make sure you import this dependency
expect(output).toEqual(expected);
});
it('should call fetch...blabla', ()=> {
output = saga.next(channel_name).value; //include channel_name so it is avaiable on the next iteration
let expected = call(fetch,'/api/messages/'+channel_name); //do all the mock you ned for this
expect(output).toEqual(expected);
});
/*here comes you answer*/
it('should take response.ok into the if statemenet', ()=> {
//your json yield is out the redux-saga context so I dont assert it
saga.next(response).value; //same as before, mock it with a ok property, so it is available
output = saga.next(res).value; //assert the put effect
let expected = put(receiveMessages(res,channel.payload,date)); //channel should be mock from previous test
expect(output).toEqual(expected);
});
});
请注意,您的代码可能做了更多我不知道的事情,但这至少应该让您有时间来解决您的问题。您可能需要为此使用一个帮助程序库,例如 免责声明:我编写此库是为了解决完全相同的问题 对于您的具体示例,使用Jest,但对摩卡的效果相同,我将做两件事: 首先,我将API调用分离到不同的函数 然后我将使用redux saga测试以同步方式测试您的逻辑: 代码如下:
import sagaHelper from 'redux-saga-testing';
import { call, put } from 'redux-saga/effects';
import { requestMessages, receiveMessages, rejectMessages } from './my-actions';
const api = url => fetch(url).then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error(response.status); // for example
}
});
function* onFetchMessages(channel) {
try {
yield put(requestMessages())
const channel_name = channel.payload
const res = yield call(api, '/api/messages/'+channel_name)
const date = moment().format('lll');
yield put(receiveMessages(res,channel.payload,date))
} catch (error){
yield put(rejectMessages(error))
}
}
describe('When testing a Saga that throws an error', () => {
const it = sagaHelper(onFetchMessages({ type: 'foo', payload: 'chan1'}));
it('should have called the API first, which will throw an exception', result => {
expect(result).toEqual(call(api, '/api/messages/chan1'));
return new Error('Something went wrong');
});
it('and then trigger an error action with the error message', result => {
expect(result).toEqual(put(rejectMessages('Something went wrong')));
});
});
describe('When testing a Saga and it works fine', () => {
const it = sagaHelper(onFetchMessages({ type: 'foo', payload: 'chan2'}));
it('should have called the API first, which will return some data', result => {
expect(result).toEqual(call(api, '/api/messages/chan2'));
return 'some data';
});
it('and then call the success action with the data returned by the API', result => {
expect(result).toEqual(put(receiveMessages('some data', 'chan2', 'some date')));
// you'll have to find a way to mock the date here'
});
});
你会在上找到很多其他更复杂的例子,这里有一个相关的问题:在redux saga文档中,他们有take正在监听多个动作的例子。基于此,我编写了一个auth saga,大致如下您可能会发现这是redux saga文档中示例的修改版本:
我不认为在处理Sagas时这是一种反模式,并且代码在测试环境Jest之外确实按预期运行。但是,我看不到在这种上下文中处理if语句的方法。这是怎么回事?谢谢,用ok属性模仿回应似乎是个计划,但你会怎么做?如果你能把最后一部分编码出来,那就太好了,因为这才是真正重要的部分传递一个像saga.next{ok:true}.value这样的对象
function* mySaga() {
while (true) {
const initialAction = yield take (['AUTH__LOGIN','AUTH__LOGOUT']);
if (initialAction.type === 'AUTH__LOGIN') {
const authTask = yield fork(doLogin);
const action = yield take(['AUTH__LOGOUT', 'AUTH__LOGIN_FAIL']);
if (action.type === 'AUTH__LOGOUT') {
yield cancel(authTask);
yield call (unauthorizeWithRemoteServer)
}
} else {
yield call (unauthorizeWithRemoteServer)
}
}
}