title: Unit testing with Bookshelf.js and knex.js
body: <p>I'm relatively new to Node and am working on a project using knex and bookshelf. I'm having a little bit of trouble unit testing my code and I'm not sure what I'm doing wrong. </p>
  
  <p>Basically I have a model (called VorcuProduct) that looks like this:</p>
  
  <pre><code>var VorcuProduct = bs.Model.extend({
      tableName: 'vorcu_products'
  });
  
  module.exports.VorcuProduct = VorcuProduct
  </code></pre>
  
  <p>And a function that saves a VorcuProduct if it does not exist on the DB. Quite simple. The function doing this looks like this:</p>
  
  <pre><code>function subscribeToUpdates(productInformation, callback) {
    model.VorcuProduct
      .where({product_id: productInformation.product_id, store_id: productInformation.store_id})
      .fetch()
      .then(function(existing_model) {
          if (existing_model == undefined) {
              new model.VorcuProduct(productInformation)
                  .save()
                  .then(function(new_model) { callback(null, new_model)})
                  .catch(callback);
          } else {
              callback(null, existing_model)
          }
      })
  }
  </code></pre>
  
  <p>Which is the correct way to test this without hitting the DB? Do I need to mock <code>fetch</code> to return a model or undefined (depending on the test) and then do the same with <code>save</code>? Should I use rewire for this?</p>
  
  <p>As you can see I'm a little bit lost, so any help will be appreciated. </p>
  
  <p>Thanks!</p>