Skip to content

Instantly share code, notes, and snippets.

@kenhoff
Created January 9, 2016 21:00
Show Gist options
  • Save kenhoff/a201b1e5fc9cf3b1c98a to your computer and use it in GitHub Desktop.
Save kenhoff/a201b1e5fc9cf3b1c98a to your computer and use it in GitHub Desktop.
Stubbing RethinkDB calls with Sinon
var r = require('rethinkdb');
dbCall = function(cb) {
r.connect(function(err, conn) {
// console.log("connected");
r.table('tv_shows').get(1).run(conn, function(err, result) {
cb(err, result)
})
})
}
module.exports = dbCall
var sinon = require('sinon');
var assert = require('chai').assert;
dbCall = require("../dbCall")
describe("db-call", function() {
it("unstubbed", function(done) {
dbCall(function(err, result) {
assert(!err)
assert(result.name == "Farscape")
assert(result.id == 1)
done()
})
})
it("stubbed", function(done) {
var r = require('rethinkdb');
sinon.stub(r, "connect").yields()
sinon.stub(r, "table").returns({
get: function() {
return {
run: function(conn, cb) {
cb(null, {
id: 100,
name: "Star Trek: The Next Generation"
}) // insert stubbed data here
}
}
}
})
dbCall(function(err, result) {
assert(!err, "db call returned error")
assert(result.name == "Star Trek: The Next Generation", "db call didn't return the right name")
assert(result.id == 100, "db call didn't return the right ID")
done()
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment