Testing modules

I have created a sails js application. I want to add unit testing for my application. I am using the following approach to conduct unit testing. http://sailsjs-documentation.readthedocs.org/en/latest/concepts/Testing/

I am using grunt to test my mocha application. Now I need a way to override some of the sail methods (find, update, etc.) to use for my testing, since I don't want to interact with the database when testing the code. Is there a way to override the sails js. As an example, when I use the User.find method in my testing, I want a specific result to test that my other methods are working fine. Any help would be appreciated.

+3


source to share


1 answer


If you want to do testing for a production database, you can use a different table / collection in your database. This is a common approach when testing integration (not using a mock).

Here is an example, in the file bootstrap.test.js



var Sails = require('sails'),
    sails;

before(function (done) {
  Sails.lift({
    connections: {
      mongodbServer: {
        database: 'database_test'
      }
    },
    models     : {
      migrate: 'drop'
    }
  }, function (err, server) {
    sails = server;
    if (err) return done(err);

    done();
  });
});

after(function (done) {
  // here you can clear fixtures, etc.
  sails.lower(done);
});

      

Your connection was supposed to use mongodbServer

db as well database_test

. Customize it according to your needs.

+3


source







All Articles