Jestjs Redux saga测试用例-jest.spyOn

Jestjs Redux saga测试用例-jest.spyOn,jestjs,redux-saga,Jestjs,Redux Saga,我使用jest.spyOn()和mockImplementation将未定义为模拟api调用的响应 佐贺 } Saga.test.tsx })) 任何帮助都将不胜感激。这件事很简单,但我一直坚持这一点。当将sagas作为纯生成器进行测试时,redux saga中间件并没有连接起来驱动生成器-您必须自己来做。因此,实际上不会调用SkillRepository.getAllSkills,而是需要将预期值提供给generator.next()。您可以在测试部分的第一个示例中看到这一点,其中调用了gen

我使用jest.spyOn()和mockImplementation将未定义为模拟api调用的响应

佐贺

}

Saga.test.tsx

}))


任何帮助都将不胜感激。这件事很简单,但我一直坚持这一点。

当将sagas作为纯生成器进行测试时,redux saga中间件并没有连接起来驱动生成器-您必须自己来做。因此,实际上不会调用
SkillRepository.getAllSkills
,而是需要将预期值提供给
generator.next()
。您可以在测试部分的第一个示例中看到这一点,其中调用了
gen.next(chooseColor(color))
来驱动生成器。在您的情况下,您可以执行以下操作:

expect(generator.next().value).toEqual(call(SkillRepository.getAllSkills));
expect(generator.next(skills).done).toBe(true);
因为实际上是redux saga中间件在执行
调用
效果,如果您想测试
SkillRepository.getAllSkill
在运行saga时被调用,您需要遵循
测试完整saga
下的示例,其中
runSaga
用于驱动
redux saga
生成器,并且您可以在其中模拟/监视模块以查看它们是否被调用

因此,我将把您的测试重新编写为:

it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const spy = jest.spyOn(SkillRepository, 'getAllSkills');
    spy.mockImplementation((): any => {
        return (skills);
    });
    runSaga(
      {dispatch: () => {}, getState: () => ({})}, // flesh these out if need be
      parseUserData, action
    ).toPromise().then(() => {
      expect(SkillRepository.getAllSkills.mock.calls.length).toBe(1);
      spy.mockRestore();
    )
});
编辑

对于上面共享的完整传奇的单元测试:

describe('ParseUserData job saga', () => {
const action = {
    type: bulkUploadActionType.GET_API_DATA,
    payload: {
        sheetData: {},
        isAdvisor: true,
    },
};
const generator = parseUserData(action);
it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const industries = { industryGroups: [] };

    expect(generator.next().value).toEqual(
      call(SkillRepository.getAllSkills));
    expect(generator.next(skills).value).toEqual(
      call(IndustryRepository.getAllIndustry));
    expect(generator.next(industries).value).toEqual(
      put(getSkillsSuccess(skills.skillGroups)));
    expect(generator.next().value).toEqual(
      put(getIndustriesSuccess(industries.industryGroups))
    );
    expect(generator.next().done).toBe(true);

});

但当我期待这一点时,我收到的是错误的<代码>预期(生成器。下一步(技能)。完成)。toBe(真)接收错误。但是是的,我现在已经掌握了关于传奇的技能,但是测试用例失败了。你在完整传奇上面发布的
parseUserData
代码是不是?如果有更多的影响,你没有包括,那么数据将不会做。我想象你正在用
getAllSkills
的结果做一些事情。是的,我正在使用结果。我已经更新了传奇故事,请看。更新在我的帖子底部。看起来您的
parseUserData
生成器有一些输入错误-在执行成功操作之前,应该是
if
语句中的
skills.skillGroups
industry.industryGroups
it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const spy = jest.spyOn(SkillRepository, 'getAllSkills');
    spy.mockImplementation((): any => {
        return (skills);
    });
    runSaga(
      {dispatch: () => {}, getState: () => ({})}, // flesh these out if need be
      parseUserData, action
    ).toPromise().then(() => {
      expect(SkillRepository.getAllSkills.mock.calls.length).toBe(1);
      spy.mockRestore();
    )
});
describe('ParseUserData job saga', () => {
const action = {
    type: bulkUploadActionType.GET_API_DATA,
    payload: {
        sheetData: {},
        isAdvisor: true,
    },
};
const generator = parseUserData(action);
it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const industries = { industryGroups: [] };

    expect(generator.next().value).toEqual(
      call(SkillRepository.getAllSkills));
    expect(generator.next(skills).value).toEqual(
      call(IndustryRepository.getAllIndustry));
    expect(generator.next(industries).value).toEqual(
      put(getSkillsSuccess(skills.skillGroups)));
    expect(generator.next().value).toEqual(
      put(getIndustriesSuccess(industries.industryGroups))
    );
    expect(generator.next().done).toBe(true);

});