Created
April 7, 2016 22:00
-
-
Save brunops/d510bb9410eae2411e10e766ff7a7bce to your computer and use it in GitHub Desktop.
Examples of testing promise controller steps with and without bind
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
const steps = { | |
stepBind(req, foo) { | |
// bla | |
}, | |
stepNoBind: (req) => (foo) => { | |
// bla | |
} | |
} | |
// controller | |
app.get('/foo', (req, res) => { | |
q({}) | |
.then(steps.stepBind.bind(null, req)) | |
.then(steps.stepNoBind(req)) | |
.catch(errorHandler) | |
}) | |
// tests | |
import sinon from 'sinon' | |
import Steps from './controller-steps' | |
import Controller from './controller' | |
describe('controller', () => { | |
let req, | |
sandbox | |
beforeEach(() => { | |
req = {} | |
sandbox = sinon.sandbox.create() | |
sandbox.stub(Steps, 'stepNoBind') | |
// can't just stub `stepBind`, because the called function is the the *return* of `.bind` | |
// need to stub `.bind` instead | |
sandbox.stub(Steps.stepBind, 'bind') | |
Controller.get(req) | |
}) | |
afterEach(() => { | |
sandbox.restore() | |
}) | |
describe('#get', () => { | |
it('passes `req` to `stepBind`', () => { | |
sinon.assert.calledWith(Steps.stepBind.bind, null, req) | |
}) | |
it('passes `req` to `stepNoBind`', () => { | |
sinon.assert.calledWith(Steps.stepNoBind, req) | |
}) | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Agora são equivalentes o wrapper e bind.