Skip to content

Instantly share code, notes, and snippets.

@shanelau
Created November 25, 2014 01:40
Show Gist options
  • Save shanelau/feea10958401749a3560 to your computer and use it in GitHub Desktop.
Save shanelau/feea10958401749a3560 to your computer and use it in GitHub Desktop.
sails test before
var Sails = require('sails');
var app;
before(function(done) {
Sails.lift({
// configuration for testing purposes
log: {
level: 'error'
},
// send test database connections down if needed
adapters: {
mysql: {
module: 'sails-mysql',
host: '127.0.0.1',
user: 'liux',
password: 'liux',
database: 'sail_test'
}
}
}, function(err, sails) {
if (err) return done(err);
// here you can load fixtures, etc.
// save reference for teardown function
app = sails;
done(err, sails);
});
});
after(function(done) {
// here you can clear fixtures, etc.
app.lower(done);
});
var bootstrap = require('../../bootstrap.test.js');
var should = require('should');
describe.only('UsersModel', function() {
describe('#find()', function() {
it('should check find function', function (done) {
sails.models.user.find()
.then(function(results) {
// some tests
results.length.should.above(0);
done();
})
.catch(done);
});
});
describe('#User find', function() {
it('find all user', function (done) {
var User = sails.models.user;
User.find({name: 'mx'}).populate('address',{city:'changsha'}).exec(function findCB(err, found) {
found.should.be.ok;
done();
});
});
});
describe('#User create', function() {
it('create user', function (done) {
var User = sails.models.user;
var peo = {
name: '刘兴',
email: '[email protected]',
passwod: '123456'
};
User.create(peo).exec(function (err, created) {
console.log(err);
console.log(created);
done();
});
});
});
describe('#address', function() {
it('create user address', function (done) {
var User = sails.models.user;
User.findOrCreate({name:'刘兴'}).exec(function (err,data) {
var address = {
province: 'hn',
city: '珠海'
}
data.address.add(address);
data.save(function (err, data) {
// console.log(data);
done();
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment