I have successfully used Knex to connect to the database. But I want to be able to unit test my code. Is there a way to trick a database connection?
I tried using proxyquire , but I can't get it to work.
The problem seems to be related to Knex initialization.
var knex = require('knex')({ client: 'mysql', connection: {} });
I install knex to taunt in my unit test.
myService = proxyquire('../app/myService', { 'knex': knexProxy });
There is knex in my service.
var knex = require('knex').knex,
When my service starts a request, it fails.
var sql = knex("table_name"); sql.insert(rowToInsert, "auto_increment_id"); sql.then(function (insertId) { resolve(); }, function (err) { reject(err); });
For some reason, I just can't capture the request before it tries to establish a connection.
I also tried creating a custom Knex Client , but that still didn't work.
Trevor allred
source share