如何使用摩卡测试嵌套的ES6发电机? [英] How Do I Test nested ES6 Generators using Mocha?
问题描述
我正在尝试使用 co-mocha
来测试我的 koa
应用程序中的一些嵌套生成器功能。类在运行时工作正常,但是当我尝试测试功能时,我无法让我的测试中嵌套的生成器运行。
正在测试的类:
import'bluebird'的承诺
class FooService {
_doAsync(){
return new Promise((resolve)=> {
setTimeout(()=> {
resolve({
foo:'FOO'
})
},500)
})
}
create(){
console.log('永远不会记录')
let self = this
return function *(){
console.log(`这不是两个)
return yield self._doAsync()
}
}
}
导出默认新FooService()
测试文件
import fooService'../services/foo-service'
pre>
import Chai from 'chai'
let expect = Chai.expect
describe('Testing generator',()=> {
it('should just work',function *(){
console.log('这个日志永远不会发生')
let result = yield fooService .create()
expect(result).to.equal({foo:'FOO'})
})
})
我正在运行mocha w /
- 需要co-mocha
和Node 4.2.6
当测试完成w / o错误时,上面的控制台的NONE已经被记录下来,所以我确信实际的测试发生器根本不运行。
如果我尝试使用npm软件包,
mocha-generators
,而我从测试生成器内部获取日志,从create()
方法返回的生成器从不触发...
我做错了什么? ?
解决方案没有
mocha-generators
,它
callback返回一个不会由任何人运行的生成器。您需要手动将其包装在co </ code>中,以便摩卡会收到承诺。
使用
mocha-generators
,您的生成器被执行,但产生一个生成器函数。这不是预期的,你应该屈服于承诺。您需要调用生成器函数,create()
调用返回自己,然后您不应该yield
a发电机本身,而是通过 <$ c $委托给它c> yield * :let result = yield * fooService.create() ();
I'm trying to use
co-mocha
to test some nested generators functionality in mykoa
app. The class works just fine at runtime, but when I attempt to test the functionality, I cannot get the nested generator to run in my test.Class being tested:
import Promise from 'bluebird' class FooService { _doAsync(){ return new Promise((resolve) => { setTimeout(() => { resolve({ foo: 'FOO' }) }, 500) }) } create(){ console.log('This never gets logged') let self = this return function*(){ console.log(`This doesn't either`) return yield self._doAsync() } } } export default new FooService()
Test File
import fooService '../services/foo-service' import Chai from 'chai' let expect = Chai.expect describe('Testing generators', () => { it('Should just work', function *(){ console.log('This log never happens') let result = yield fooService.create() expect(result).to.equal({foo: 'FOO'}) }) })
I'm running mocha w/
--require co-mocha
and Node 4.2.6While the tests complete w/o errors, NONE of the console above ever get logged and so I'm quite sure the actual test generator is never running at all.
If I try using the npm package,
mocha-generators
instead, while I get the log from inside the test generator, the underlying generator returned from thecreate()
method on the service never fires...What am I doing wrong??
解决方案Without
mocha-generators
, theit
callback returns a generator that will not be run by anyone. You'd need to wrap it inco
manually so that mocha would receive a promise.With
mocha-generators
, your generator is executed but yields a generator function. That's not expected, you are supposed to yield promises. You need to call the generator function that thecreate()
call returns yourself, and then you shouldn'tyield
a generator itself but rather delegate to it viayield*
:let result = yield* fooService.create()();
这篇关于如何使用摩卡测试嵌套的ES6发电机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!