Created
October 22, 2012 19:10
-
-
Save pchw/3933453 to your computer and use it in GitHub Desktop.
use sinon.stub for a method has callback argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
should = require 'should' | |
sinon = require 'sinon' | |
class Hoge | |
fuga: (params, callback)-> | |
return callback(params) | |
class Fuga | |
moge: (params, dummy, callback)-> | |
return callback(params) | |
describe 'sinon sample', -> | |
it 'callback stub sample(OK)', (done)-> | |
hoge = new Hoge() | |
stub = sinon.stub(hoge, 'fuga').yields({'foo':'bar'}) | |
hoge.fuga({'foo': 'bar'}, | |
(params)-> | |
params.should.have.property 'foo' | |
done() | |
) | |
it '[NG] callback stub sample(assertion failed)', (done)-> | |
hoge = new Hoge() | |
stub = sinon.stub(hoge, 'fuga').yields({'fuga':'bar'}) | |
hoge.fuga({'foo': 'bar'}, | |
(params)-> | |
# AssertionError: expected { fuga: 'bar' } to have a property 'foo' | |
params.should.have.property 'foo' | |
done() | |
) | |
it '[NG] callback not called', (done)-> | |
# Error: timeout of 2000ms exceeded | |
hoge = new Hoge() | |
stub = sinon.stub(hoge, 'fuga') | |
hoge.fuga({'foo': 'bar'}, | |
(params)-> | |
params.should.have.property 'foo' | |
done() | |
) | |
describe 'sinon sample(Fuga)', -> | |
it '[OK] callback stub sample include dummy', (done)-> | |
fuga = new Fuga() | |
stub = sinon.stub(fuga, 'moge').yields({'foo':'bar'}) | |
fuga.moge({'foo': 'bar'}, null, | |
(params)-> | |
params.should.have.property 'foo' | |
done() | |
) | |
it '[NG] callback stub sample include dummy', (done)-> | |
fuga = new Fuga() | |
stub = sinon.stub(fuga, 'moge') | |
fuga.moge({'foo': 'bar'}, null, | |
(params)-> | |
params.should.have.property 'foo' | |
done() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment