jbrumwell / mock-knex

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

Mocking knex transaction using mock-knex module #60

Closed ShaileshDevadiga closed 7 years ago

ShaileshDevadiga commented 7 years ago

Hi, I want to mock the knex transaction using mock-knex module.

Code:

knex.transaction((trx) => { knex(tableName) .transacting(trx) .forUpdate() .select('ProductId','ProductName','ProductStatus') .where({ProductStatus: 'Available'}) .orderBy('DATE_ADDED', 'asc') .limit(1) .then((data) => { return knex(tableName).transacting(trx) .where(ProductId: data[0].ProductId) .update({ ProductStatus : "Booked", }); }) .then(trx.commit) .catch(trx.rollback); }) .then((response) => { console.log('Transaction complete - ', JSON.stringify(response)); }) .catch((error) => { console.error('Error - ', JSON.stringify(error)); });

Can anyone suggest me how to mock this?

jbrumwell commented 7 years ago

@ShaileshDevadiga You can take a look at https://github.com/colonyamerican/mock-knex/blob/master/test/common/bookshelf.js#L168 for some inspiration on how to get started :)

You'd have to make sure knex is mocked and tracker is installed then you can use the query to determine what results to code is looking for:

tracker.on('query', (query, step) => {
  console.log(step, query.sql);
});

knex.transaction((trx) => {
  // Your code here
});