0

我正在测试以下传奇功能

export function* fetchMessages(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))
    }
}

这些是我对第 3 次和第 4 次产量的测试:

const gen = onFetchMessages(fetchMessages(channel))
////where export const fetchMessages = createAction(LOAD_MESSAGES);
const channel = "channel 9"
const response = []

    assert.deepEqual(
        gen.next(response).value,
        response.json,
        'should convert response to json'
    )

    const date = Date.now();
    assert.deepEqual(
        gen.next(response,channel,date).value,
        put(receiveMessages(response,channel,date)),
        'should dispatch LOAD_MESSAGES_SUCCESS with payloads of response, channel and date'
    )

实际返回未定义,即 gen.next(response,channel,date).value 返回未定义。你能告诉我测试这些的正确方法吗?谢谢

4

1 回答 1

0

不太确定您的代码试图做什么。此功能的正确测试可能如下所示:

import { put, call } from 'redux-saga/effects';

// success route
const arg = fetchMessage(channel);
let gen = onFetchMessage(arg);
assert.deepEqual(
  gen.next().value,
  put(requestMessages()),
  'should ...'
);

assert.deepEqual(
  gen.next().value,
  call(fetch, 'api/messages/' + arg.payload),
  'should ...'
);

assert.deepEqual(
  gen.next({ ok: true, json: () => 'test data' }).value,
  'test data',
  'should ...'
);

assert.deepEqual(
  gen.next('test res').value,
  put(receiveMessage('test res', arg.payload, moment().format('lll')), // you may need to get date from args, if date does not match
  'should ...'
);

assert.deepEqual(
  gen.next().done,
  true,
  'should ...'
);

// unsuccessful route
const arg = fetchMessage(channel);
let gen = onFetchMessage(arg);
gen.next();
gen.next();

assert.deepEqual(
  gen.next({ ok: false, json: () => 'test data' }).done,
  true,
  'should ...'
);

// exception route
const arg = fetchMessage(channel);
let gen = onFetchMessage(arg);
gen.next();
gen.next();

assert.deepEqual(
  gen.throw('some error').value,
  put(rejectMessage('some error'),
  'should ...'
);
于 2016-09-23T15:17:14.747 回答