Skip to content

A mock knex adapter for simulating a database during testing

License

Notifications You must be signed in to change notification settings

colmaengus/mock-knex

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-knex

A mock knex adapter for simulating a database during testing, exspecially usefull when used in combination with fixture-factory.

Installation

$ npm install mock-knex --save-dev

Mocking Knex

Mock

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

mockDb.mock(db, '[email protected]');

Set Adapter

Avoiding setting the platform and version, each time you mock a database. Not providing a version will result in the latest version being used.

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

mockDb.setAdapter('[email protected]');

...

mockDb.mock(db);

Unmock Database

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

mockDb.setAdapter('[email protected]');

mockDb.mock(db);

... run tests ...

mockDb.unmock(db);

Tracker

The tracker allows you catch and respond to queries that occur during testing, see Test for more examples.

Example Usage with knex

var tracker = require('mock-knex').getTracker();

tracker.install();

tracker.on('query', function checkResult(query) {
  expect(query.method).to.equal('first');
  query.response([
    {
      fielda : 'A',
      fieldb : 'B'
    },
    {
      fielda : 'C',
      fieldb : 'D'
    },
    {
      fielda : 'E',
      fieldb : 'F'
    }
  ]);
});

knex.table('table').first('fielda', 'fieldb').then(function checkFirstArrResults(model) {
  expect(model.fielda).to.equal('A');
  expect(model.fieldb).to.equal('B');
  done();
});

Example Usage with bookshelf.js

var tracker = require('mock-knex').getTracker();

tracker.install();

tracker.on('query', function sendResult(query) {
  query.response([
    {
      id : 1,
      foo : 'bar'
    }
  ]);
});

Model.forge({ id : 1 }).fetch()
  .then(function fetchResult(model) {
    expect(model).to.be.an.instanceof(Model);
    expect(model.get('id')).to.equal(1);
    expect(model.get('foo')).to.equal('bar');
    done();
  });

More Examples?

Checkout the Tests

Running Tests

$ npm install
$ npm test

About

A mock knex adapter for simulating a database during testing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%