Saga单元测试deepEqual无论如何仍返回false

时间:2019-01-15 16:06:54

标签: unit-testing redux-saga

我对Redux-saga相当陌生,正在从事单元测试。 API调用功能单元测试代码即使看起来相同也返回false。我想念什么?

[操作]

export const actions = {
  readUsers: {
    read: (data) => util.createAction('READ_USER', data),
    request: () => util.createAction('READ_REQUEST'),
    success: (data: any) => util.createAction('READ_SUCCESS', data),
    failure: (error: any) => util.createAction('READ_FAILURE', error)
  }
};

[发电机功能]

export function* fetchUsers(action) {
  yield put(actions.readUsers.request());
  try {
    let data = {id: action.payload.id, option: action.payload.option};
    let response = yield call(API().getUsers, data);
    if (response) {
      yield put(actions.readUsers.success(response.users));
    } else {
      yield put(actions.readUsers.failure(response.error));
    }
  } catch (error) {
    yield put(actions.readUsers.failure(error));
  }
};

[单元测试]

const id = 'user-id';
const option = {};
const result = {users: []};
const action = {payload : {id, option}};
describe('user-saga', () => {
  describe('fetchUsers', () => {
    const generator = fetchUsers(action);
    it('should return actions.readUsers.request()', () => {
      assert.deepEqual(generator.next().value, put(actions.readUsers.request()));
    }); // PASS

    it('should return the Api.getUsers call', () => {
      assert.equal(generator.next(result).value, call(Api().getUsers, action.payload));
    }); // FAIL

    it('should return actions.readUsers.success()', () => {
      assert.deepEqual(generator.next(result).value, put(actions.readUsers.success(result)));
    }); // FAIL

    it('should be finished', () => {
      assert.deepEqual(generator.next().done, true);
    }); // PASS
  })
});

[测试输出]

第一个测试通过。

第二次测试失败,并显示此错误消息

 AssertionError: { '@@redux-saga/IO': true,
    CALL: { context: null, fn: [Function: getUsers], args: [ [Object] ] } } deepEqual { '@@redux-saga/IO': true,
    CALL: { context: null, fn: [Function: getUsers], args: [ [Object] ] } }

第三次测试失败,并显示此错误消息

AssertionError: undefined deepEqual { '@@redux-saga/IO': true,
 PUT: { channel: null,
       action: { type: 'READ_SUCCESS', users: undefined } } }

0 个答案:

没有答案