【问题标题】:Unit testing redux-saga select单元测试 redux-saga 选择
【发布时间】:2019-06-17 13:38:06
【问题描述】:

我有一个这种形式的简单传奇:

const getAccountDetails = function * () {
   const { url } = yield select(state => state.appConfig)
   const accountDetails = yield call(apiFetchAccountDetails, url)
}

我正在尝试编写单元测试:

describe('getAccountDetails', () => {
    const iterator = getAccountDetails()
    it("should yield an Effect 'select(state=> state.appConfig)'", () => {
       const effect = iterator.next().value
       const expected = select(state => state.appConfig)
       expect(effect).to.deep.eql(expected)
    })

此测试失败。尽管effect 和expected 非常相似,但它们并不完全相同。

payload.selector.scopes 中至少有一个差异,其中产生的效果和预期如下:

由于这两者的范围总是不同的,如何才能使这些测试起作用?

eta:此模式改编自 the example 链接到 redux-saga 文档

【问题讨论】:

    标签: mocha.js generator chai redux-saga


    【解决方案1】:

    在回来的路上找到this issue后破解了。

    解决方法是创建一个命名函数来执行选择并将其从被测 saga 所在的模块中导出,然后在测试中使用相同的函数。一切都很好。

    export const selectAppConfig = state => state.appConfig
    
    const getAccountDetails = function * () {
       const { url } = yield select(selectAppConfig)
       const accountDetails = yield call(apiFetchAccountDetails, url)
    }
    
        import {selectAppConfig} from './sagaToTest'
    
        describe('getAccountDetails', () => {
            const iterator = getAccountDetails()
            it("should yield an Effect 'select(state=> state.appConfig)'", () => {
               const effect = iterator.next().value
               const expected = select(selectAppConfig)
               expect(effect).to.deep.eql(expected)
        })
    
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多