我正在使用Mocha测试一些Node.js代码,并想用它process.nextTick()
来调用方法的回调。
编码
@getNouns: (callback) ->
@_wordnik.randomWords(
includePartOfSpeech: 'noun',
(e, result) ->
throw new Error('Wordnik Failed') if e
process.nextTick ->
callback(result)
)
考试
it 'should call a callback with the response', (done) ->
sinon.stub(Word._wordnik, 'randomWords').yields(null, [
{id: 1234, word: "hello"},
{id: 2345, word: "foo"},
{id: 3456, word: "goodbye"}
]
)
spy = sinon.spy()
Word.getNouns (result) -> spy(result); done(); null
expect(spy).have.been.calledWith [
{id: 1234, word: "hello"},
{id: 2345, word: "foo"},
{id: 3456, word: "goodbye"}
]
由于某种原因,done()
当我运行 mocha 时,我得到了一个被调用两次的错误。如果我在process.nextTick()
.