/controller.ts Secret
Created
March 11, 2018 16:59
Star
You must be signed in to star a gist
Sinon and Knex Issues
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
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) | |
); |
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
getKnex = (tenantId) => { | |
if (!this.connections[tenantId]) { | |
logger.error('could not find database connection'); | |
} else { | |
return this.connections[tenantId].knex; // an existing knex instance | |
} | |
} |
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
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