Created
April 10, 2014 00:40
-
-
Save chrisabrams/10333707 to your computer and use it in GitHub Desktop.
Promises with stubs in Mocha
This file contains hidden or 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
chai = require('chai') | |
sinonChai = require('sinon-chai') | |
chai.use(sinonChai) | |
sinon = require('sinon') | |
expect = chai.expect | |
Q = require('q') | |
var db = { | |
query: function(sql) { | |
d = Q.defer() | |
d.resolve([{user: 'chris', id: 1}]) | |
return d.promise | |
} | |
} | |
describe('Promise with stubs', function() { | |
it('should stub a method that returns a promise', function(done) { | |
querySpy = sinon.stub(db, 'query').returns(Q.resolve([{user: 'derek', id: 2}])) | |
querySpy('SELECT * FROM users').done(function(result) { | |
expect(result[0].id).to.be.equal(2) | |
db.query.restore() | |
done() | |
}) | |
}) | |
it('should stub a method (with arguments) that returns a promise', function(done) { | |
querySpy = sinon.stub(db, 'query').withArgs('SELECT * FROM users').returns(Q.resolve([{user: 'derek', id: 2}])) | |
querySpy('SELECT * FROM users').done(function(result) { | |
expect(result[0].id).to.be.equal(2) | |
db.query.restore() | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment