Unit testing Redux传奇:抛出并停止生成器
我在写发电机。我正在测试它。它检查是否定义了Unit testing Redux传奇:抛出并停止生成器,unit-testing,redux,redux-saga,Unit Testing,Redux,Redux Saga,我在写发电机。我正在测试它。它检查是否定义了window.ethereum。如果不是,它应该抛出并停止。基本上应满足以下测试: describe('handle initialize Web3 saga', async assert => { global.window = {} assert({ given: 'nothing, the window object', should: 'have no property called Web3', act
window.ethereum
。如果不是,它应该抛出并停止。基本上应满足以下测试:
describe('handle initialize Web3 saga', async assert => {
global.window = {}
assert({
given: 'nothing, the window object',
should: 'have no property called Web3',
actual: window.web3,
expected: undefined
})
const gen = cloneableGenerator(handleInitializeWeb3)()
{
// The important parts are in this block scope
const clone = gen.clone()
assert({
given: 'window.ethereum undefined',
should: 'throw',
actual: clone.next().value.message,
expected: '[WARNING]: window.ethereum has no provider!'
})
assert({
given: 'nothing',
should: 'be done',
actual: clone.next().done,
expected: true
})
}
class Provider {}
window.ethereum = new Provider()
// ... more tests
})
下面是我如何尝试实现它的
function* handleInitializeWeb3() {
if (!window.ethereum) {
yield new Error('[WARNING]: window.ethereum has no provider!')
}
// ... more yields
}
但这一传奇并没有停止。在测试中,
应该:“完成”
失败,saga将从yield
s返回if
语句之外的值。当抛出错误时,我如何才能通过这些测试并停止传奇?生成错误实例与生成任何其他值(即生成器保持运行)的行为相同。如果你想停止生成器,你应该抛出新的错误(…
,就像在正常函数中一样
如果出于某种原因,您不想抛出一个错误实例,并且确实想产生一个错误实例,然后停止,只需在产生错误后返回;