Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created March 11, 2018 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save christopherhill/33363c8c980fce83a6617e91d4c18e39 to your computer and use it in GitHub Desktop.
Sinon and Knex Issues
query = knex(tableName).modify(envelope, queryString);
// envelope conditionally calls .where, .select, etc. chainable methods on Knex to limit query based on query string
query.then(
(data) => responseMap.getAll(req, res, data),
(err) => responseMap.error(req, res, data)
);
getKnex = (tenantId) => {
if (!this.connections[tenantId]) {
logger.error('could not find database connection');
} else {
return this.connections[tenantId].knex; // an existing knex instance
}
}
it('should return a valid response', (done) => {
const knex: any = sinon.stub();
// these are the chainable methods that need to return a copy of the knex instance
knex.column = sinon.stub().returns(knex);
knex.from = sinon.stub().returns(knex);
knex.where = sinon.stub().returns(knex);
knex.select = sinon.stub().returns(knex);
knex.where = sinon.stub().returns(knex);
knex.modify = sinon.stub().returns(knex);
knex.onFirstCall().resolves(authMock.authorization); // this is for an authorization call (that comes ahead of the controller)
knex.onSecondCall().resolves(controllerMock.getAll);
const getKnex = sinon.stub(db, 'getKnex').returns(knex);
chai.request(app)
.get('/api/v1/route')
.set('Authorization', authToken)
.end((err, res) => {
// query.restore();
console.log('data: ', res.body.data[0]);
chai.expect(res.status).to.equal(200);
chai.expect(res.body.success).to.equal(true);
chai.expect(res.body.data).to.be.an('array');
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment