Testing sails.js with a mocha: cannot find / api / services / myService

consider this mocha test:

var Sails = require('sails');

describe("Foo Model creation:", function() {
  // create a variable to hold the instantiated sails server
  var app;

  // Global before hook
  before(function(done) {
    // Lift Sails and start the server
    Sails.lift({
      log: {
        level: 'error'
      }
    }, function(err, sails) {
      app = sails;
      done(err, sails);
    });
  });

  // Global after hook
  after(function(done) {
    app.lower(done);
  });

  describe("new foo", function() {
    var foo;
    before(function (cb) {
      var fooData = {
        name: "test foo to be removed after test"
      };

      Foo.create(fooData, function (err, newFoo)              
        if (err) return cb(err);
        foo = newFoo;
        cb();
      });
    });
    it("must show the name", function() {
      foo.must.have.property('name');
    });

    after(function (cb){
      foo.destroy(function (err) {
        cb(err);
      });
    });
  });
})

This will work, except that the Foo model depends on the sail service, i.e. library code defined in / api / services. Sails, when hoisting here, cannot find these services.

Is there a way to instruct the sails during a mocha test to load services?

+3
source share
1 answer

I had a similar problem, and indeed, Sails should load services, controllers, etc.

There were some debugging tools that helped in raising Sails:

Sails.lift({ log: { level: 'verbose' }, appPath: '../', // explicitly load almost everything but policies //loadHooks: ['moduleloader', 'userconfig', 'orm', 'http', 'controllers', 'services', 'request', 'responses', 'blueprints'] }

I also did a live debugging of Mocha (although I will not go into some steps about this).

- verbose ( silly, verbose ) , - . appPath "../", , , voila, sails.services.myService!

, !

0

All Articles