jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
240 stars 71 forks source link

confused by usage #61

Closed dagda1 closed 7 years ago

dagda1 commented 7 years ago

I am confused as to how to use mock-knex. My model files and controller files contain references to bookshelf.js file like this:

const Bookshelf = require('../bookshelf').Bookshelf;
const ModelBase = require('../bookshelf').ModelBase;
const Manager = require('../models/manager');

const Result = ModelBase.extend({
  tableName: 'results',
  manager: function() {
    return this.belongsTo(Manager);
  }
});

module.exports = Bookshelf.model('Result', Result);

bookshelf.js looks like this

const config = require('./config/config').config;
const bcrypt = require('bookshelf-bcrypt');

const knex = require('knex');

const connection = knex(config.database);

if(process.env.NODE_ENV === 'test') {
  const mockKnex = require('mock-knex');
  mockKnex.mock(connection, 'knex@0.11');
}

const bookshelf = require('bookshelf')(connection);
const ModelBase = require('bookshelf-modelbase')(bookshelf);

bookshelf.plugin(['registry', 'bookshelf-camelcase']);
bookshelf.plugin(bcrypt);

module.exports.Bookshelf = bookshelf;

module.exports.ModelBase = ModelBase;

How can I use mock-knex in this scenario?

Also in the docs it says I should use sqlite like this:

var knex = require('knex');
var mockDb = require('mock-knex');
var db = knex({
    client: 'sqlite',
});

Does mock-knex use sqlite? if so, do I need to create the tables and seed the data in the sqlite database?