Unit testing with Bookshelf.js and knex.js

后端 未结 2 783
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 08:18

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

相关标签:
2条回答
  • 2020-12-14 08:54

    I have been using in-memory Sqlite3 databases for automated testing with great success. My tests take 10 to 15 minutes to run against MySQL, but only 30 seconds or so with an in-memory sqlite3 database. Use :memory: for your connection string to utilize this technique.

    A note about unit tesing - This is not true unit testing, since we're still running a query against a database. This is technically integration testing, however it runs within a reasonable time period and if you have a query-heavy application (like mine) then this technique is going to prove more effective at catching bugs than unit testing anyway.

    Gotchas - Knex/Bookshelf initializes the connection at the start of the application, which means that you keep the context between tests. I would recommend writing a schema create/destroy script so that you and build and destroy the tables for each test. Also, Sqlite3 is less sensitive about foreign key constraints than MySQL or PostgreSQL, so make sure you run your app against one of those every now and then to ensure that your constraints will work properly.

    0 讨论(0)
  • 2020-12-14 08:55

    This is actually a great question which brings up both the value and limitations of unit testing.

    In this particular case the non-stubbed logic is pretty simple -- just a simple if block, so it's arguable whether it's this is worth the unit testing effort, so the accepted answer is a good one and points out the value of small scale integration testing.

    On the other hand the exercise of doing unit testing is still valuable in that it points out opportunities for code improvements. In general if the tests are too complicated, the underlying code can probably use some refactoring. In this case a doesProductExist function can likely be refactored out. Returning the promises from knex/bookshelf instead of converting to callbacks would also be a helpful simplification.

    But for comparison here's my take on what true unit-testing of the existing code would look like:

    var rewire = require('rewire');
    var sinon = require('sinon');
    var expect = require('chai').expect;
    var Promise = require('bluebird');
    var subscribeToUpdatesModule = rewire('./service/subscribe_to_updates_module');
    
    var subscribeToUpdates = subscribeToUpdatesModule.__get__(subscribeToUpdates);
    
    describe('subscribeToUpdates', function () {
      before(function () {
        var self = this;
        this.sandbox = sinon.sandbox.create();
        var VorcuProduct = subscribeToUpdatesModule.__get__('model').VorcuProduct;
    
        this.saveStub = this.sandbox.stub(VorcuProduct.prototype, 'save');
        this.saveStub.returns(this.saveResultPromise);
    
        this.fetchStub = this.sandbox.stub()
        this.fetchStub.returns(this.fetchResultPromise);
    
        this.sandbox.stub(VorcuProduct, 'where', function () {
          return { fetch: self.fetchStub };
        })
    
      });
    
      afterEach(function () {
        this.sandbox.restore();
      });
    
      it('calls save when fetch of existing_model succeeds', function (done) {
        var self = this;
        this.fetchResultPromise = Promise.resolve('valid result');
        this.saveResultPromise = Promise.resolve('save result');
        var callback = function (err, result) {
          expect(err).to.be.null;
          expect(self.saveStub).to.be.called;
          expect(result).to.equal('save result');
          done();
        };
        subscribeToUpdates({}, callback);
      });
    
      // ... more it(...) blocks
    
    });
    
    0 讨论(0)
提交回复
热议问题