简体   繁体   English

使用磁带进行redux-saga测试

[英]redux-saga testing with tape

I am trying to do testing on redux-saga. 我正在尝试对redux-saga进行测试。 When I do generator.next(data).value for some unknown reason, the data is not passed onto the saga function as follows: 当我出于某些未知原因执行generator.next(data).value时,数据不会按以下方式传递到saga函数:

const gen = onCreateMessage()

const message = "aa"

gen.next(message).value

///SAGA FUNCTION /// SAGA功能

export function* onCreateMessage(message) {

    yield put(addMessage(message.payload))

Error message. 错误信息。 Cannot read payload of undefined. 无法读取未定义的有效负载。

The problem is that your generator function accepts the message as an argument so you need to pass that when initializing the generator: 问题在于您的生成器函数接受message作为参数,因此您需要在初始化生成器时传递该message

const gen = onCreateMessage( /* the message */ )

You'd then be able to get the next value by doing gen.next().value and verifying your assertion. 然后,您可以通过执行gen.next().value并验证您的断言来获取下一个值。

Here's a JS Bin with an example: http://jsbin.com/catisu/edit?js,console 这是一个带示例的JS Bin: http : //jsbin.com/catisu/edit? js, console

 const { put, take } = ReduxSaga.effects; const { expect } = chai; const createMessage = message => ({ type: 'CREATE_MESSAGE', payload: message, }); const addMessage = message => ({ type: 'ADD_MESSAGE', payload: message, }); function* onCreateMessage(message) { yield put(addMessage(message.payload)); } // In your test const message = 'some message'; // Message is passed when the generator is initialized const gen = onCreateMessage(createMessage(message)) let next = gen.next(); // Using Chai here but with tape it'd be the same concept try { expect(next.value).to.eql( put(addMessage(message)) ); console.log('It works!'); } catch(e) { console.error('bummer'); } 
 <script src="https://npmcdn.com/redux-saga@0.11.1/dist/redux-saga.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script> 

Also small thing, but it also looks like your generator is expecting an object with a payload key, so a plain string won't work. 这也是一件小事,但看起来您的生成器还期望带有payload密钥的对象,因此纯字符串将无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM