Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save luislobo/a9ced0fc6c9aee8663dc to your computer and use it in GitHub Desktop.
If you need also Fixtures, you can add sails-hook-fixtures, and configure it like the following:
// Require the default sails singleton (this is the same Sails instance you use globally in your app)
var sails = require('sails');
// setup
before(function(done) {
sails.lift({
// You can customize the port if you want (or use sails.load instead if you are testing using the VRI/sails.request() or just need access to the ORM, etc.)
// port: 4000,
// We usually disable the grunt hook because you don't need it when testing and it slows things down
hooks: {
grunt: false,
fixtures: require('sails-hook-fixtures')
},
fixtures: require('./fixtures'),
// You might also want to set the log to "silent"-- usually a good idea to leave it on "error" or even "warn" though.
log: {
level: 'error'
},
// Instead of using migrate:drop, you can also use the memory adapter (if you want to test with the actual database, use `migrate: drop` instead.)
connections: {
memory: {
adapter: 'sails-memory'
}
},
models: {
connection: 'memory'
},
// Utility routes to make writing your tests easier
routes: {
// Shortcut route to log user in without using the actual "/me/login" endpoint,
// which has its own test suite.
'GET /_login': function(req, res) {
User.findOne(req.param('id')).exec(function(err, user) {
if (err) return res.serverError();
if (!user) return res.notFound();
var _user = user.toJSON();
req.session.user = _user;
res.ok();
});
},
'GET /_logout': function(req, res) {
delete req.session.user;
return res.ok();
},
// more utility routes here
},
bootstrap: function(cb) {
cb();
// async.auto({
// // Load fixtures here...
// // e.g.
// // users: require('./fixtures/users.js'),
// // pets: require('./fixtures/pets.js'),
// users: require('./fixtures/users')
// }, function (err) {
// if (err) {
// console.error('ERROR IN BOOTSTRAP DURING TESTS:',err);
// return cb(err);
// }
// return cb();
// });
}
}, function(err) {
if (err) {
console.log('ERROR OCCURRED DURING LIFT:', err);
return done(err);
}
return done();
});
});
// teardown
after(function(done) {
// Tear down sails server
sails.lower(function() {
return done();
});
});
@luislobo
Copy link
Author

luislobo commented Feb 4, 2016

And you need to have a fixtures folder, with something like this:

module.exports = {
  order: ['Group', 'User', 'Rcu', 'Dashboard'],
  empty: ['Group', 'User','Dashboard'],
  Group: [{
    id: "547f9b08346c6047458ebe21",
    name: "Some Group 1",
    userList: "547f9b08346c6047458ebe11"
  }, {
    id: "547f9b08346c6047458ebe22",
    name: "Some other group",
    userList: "547f9b08346c6047458ebe12"
  }],
  User: [{
    id: "547f9b08346c6047458ebe01",
    name: "Super Admin",
    email: "super.admin@testorg.com",
    userList: "547f9b08346c6047458ebe12",
    collections: {
      group: {
        id: '547f9b08346c6047458ebe21'
      }
    }
  }, {
    id: "547f9b08346c6047458ebe02",
    name: "Test Admin User",
    email: "test.admin@testorg.com",
    userList: "547f9b08346c6047458ebe12",
    collections: {
      group: {
        id: '547f9b08346c6047458ebe22'
      }
    }
  }],
  Dashboard: [{
    id: "555e5d3940aa8b184c558b50",
    name: "Test Dashboard",
    collections: {
      group: {
        id: '547f9b08346c6047458ebe22'
      }
    }
  }]
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment