【问题标题】:redux-observable unit test epci with dependencies具有依赖关系的 redux-observable 单元测试 epci
【发布时间】:2018-01-24 03:35:52
【问题描述】:

我有一个带有以下签名的史诗(注意依赖关系)

export const incrementalSearchEpic = (action$, store, { incrementalSearchService }) => {
    // eslint-disable-next-line max-len
    return action$.ofType('SEARCH_STORES').mergeMap((action) =>
        incrementalSearchService.performIncrementalSearch(action)
    );
};

现在我需要对此进行单元测试,这是我的单元测试代码

beforeEach(() => {
    epicMiddleWare = createEpicMiddleware(incrementalSearchEpic,
        {
            dependencies: {
                incrementalSearchService: IncrementalSearchServiceMock
            }
        });
    const mockStore = configureMockStore([epicMiddleWare]);
    const getState = {}; // initial state of the store         
    store = mockStore(getState);
});

// eslint-disable-next-line max-len
it('calls the incrementalSearchService->performIncrementalSearch with the parameters when called', () => {
    //dispatch the incremental search action
    const searchStore = {
        type: 'SEARCH_STORES',
        SearchText: 'Aldi'
    };

    store.dispatch(searchStore);

    expect(store.getActions()).toEqual([
        searchStore
    ]);
});

但是当我运行代码时出现以下错误

TypeError: 无法读取未定义的属性“performIncrementalSearch”

似乎依赖项没有正确传递。

【问题讨论】:

  • 安装了什么版本的 redux-observable?
  • redux-observable version: 0.14.1 它在我运行主应用程序时工作,它只是在单元测试中失败。
  • 这里显示的一切看起来都很好,假设它与您使用的代码相同。不幸的是,我认为我不会有任何帮助。我会在史诗一开始就暂停调试器,在返回你的流之前,确认依赖关系是未定义的,然后返回调用堆栈看看为什么——你应该能够通过这种方式看到原因。

标签: unit-testing jestjs redux-observable


【解决方案1】:

我刚刚意识到:如果 redux-observable 没有注入提供的依赖项对象,你实际上会得到 Cannot read property 'incrementalSearchService' of undefined not performIncrementalSearch 因为 redux-observable 默认情况下不注入对象——它不会出错,因为它确实在传递你的对象。

相反,可能的问题是您对IncrementalSearchServiceMock 的导入或定义实际上是undefined。可能是名称错误的导入或类似名称。

如果您不能立即看到原因,请在 beforeEach 的开头暂停调试器并确认 IncrementalSearchServiceMock 的值不是未定义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多