在if语句中测试redux-saga并使用实际值

时间:2016-09-10 16:11:36

标签: 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.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,因此由于此错误消息无法达到产量。 所以我的第一个问题是'if(response.ok)',但即使我删除它,yield response.json()也会返回一个错误,此后不会执行后的yield。 如果有人能告诉我如何测试这些,将不胜感激。

3 个答案:

答案 0 :(得分:3)

将响应对象传递给上一次执行并测试条件,我会这样做,希望这会有所帮助:

 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);
 });

});

请注意,您的代码可能会做更多我不知道的事情,但这至少应该让您在某些方面解决您的问题。

答案 1 :(得分:1)

您可能希望使用帮助程序库,例如redux-saga-testing

免责声明:我写了这个库来解决同样的问题

对于您的具体示例,使用Jest(但对Mocha也一样),我会做两件事:

  • 首先,我将API调用分离为另一个函数
  • 然后我会使用redux-saga-testing以同步的方式测试你的逻辑:

以下是代码:

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'
    });
});

你会在project's GitHub.

上找到很多其他的例子(更复杂的例子)

答案 2 :(得分:0)

以下是一个相关问题:在val messageStream = env.addSource(new FlinkKafkaConsumer09("CL", new SimpleStringSchema, properties)) 文档中,他们提供了redux-saga正在侦听多个操作的示例。基于此,我写了一个看起来或多或少像这样的auth saga(您可能会认识到这是take文档中示例的修改版本:

redux-saga

在处理Sagas时,我并不认为这是一种反模式,代码肯定会在测试环境(Jest)之外按预期运行。但是,我认为无法在此上下文中处理if语句。这应该如何运作?