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 を実行すると、2 回呼び出されたというエラーが発生します。の外でコールバックを実行すると、process.nextTick()
.