Created
August 12, 2015 19:39
-
-
Save tjoskar/2076268a730ebb254d52 to your computer and use it in GitHub Desktop.
Mocking knex with mock-knex
This file contains hidden or 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
'use strict'; | |
var mockKnex = require('mock-knex'); | |
var knex = require('knex')({ | |
client: 'mysql', | |
connection: { | |
host: 'localhost', | |
user: 'user', | |
password: 'password', | |
database: 'database', | |
port: 3306 | |
}, | |
debug: false | |
}); | |
var tracker = mockKnex.getTracker(); | |
mockKnex.mock(knex, '[email protected]'); | |
tracker.install(); | |
tracker.on('query', function checkResult(query) { | |
console.assert(query.method === '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) { | |
console.log(model.fielda, 'A', model.fielda === 'A'); | |
console.assert(model.fieldb === 'B'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment