在使用mocha测试应用程序时,我想创建一些用户,以便我可以测试登录
根据Sails的文档,我们可以在中写下这个逻辑
before()函数中的bootstrap.test.js
但是下面的代码不是从bootstrap.test.js创建用户
我得到了不确定的结果。
因为登录测试失败了。
var sails = require('sails');
before(function(done) {
// Increase the Mocha timeout so that Sails has enough time to lift.
this.timeout(5000);
sails.lift({
// configuration for testing purposes
}, function(err) {
if (err) return done(err);
// as per documentation we must write that logic here,
// but it is unable to create the users (user_details is the model class).
user_details.create(
[{
id:1,
email: "vishal@gmail.com",
password: "vishal",
userType:'ADMIN'
}]
).exec(function createCB(err, user) {
logger.info('Created User');
});
done(err, sails);
});
});
after(function(done) {
// here you can clear fixtures, etc.
sails.lower(done);
});
请帮帮我。
答案 0 :(得分:0)
经过大量搜索后,我得到了以下解决方案。这将在实际测试开始之前创建记录。
var sails = require('sails');
var _ = require('lodash');
require('ts-node/register');
var async = require("async");
global.chai = require('chai');
global.should = chai.should();
before(function (done) {
this.timeout(10000);
sails.lift({
environment: 'test',
log: {
level: 'silent'
},
hooks: {
session: false
},
models: {
connection: 'unitTestConnection',
migrate: 'drop'
},
connections: {
unitTestConnection: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root',
password: 'password',
database: 'ddn_db_dev',
port: 3306
}
}
}, function(){
async.parallel({
createTenants: function(cb){
// create tenants
tenant.create(
[{
id: 1,
name: "SAAS",
organization: "SAAS",
domain: "SAAS",
email: "geocloudinc@gmail.com",
tenantType: 'SAAS',
phone: '9848022338',
address: 'USA'
}]
).exec(function createCB(err, user) {
logger.info('bootstrap.test.js From create tenant');
cb();
});
}
},
function(err, results) {
async.parallel({
createUsers: function(cb){
// create users
user_details.create(
[{
id: 1,
email: "sass_geocloudinc@gmail.com",
passwordHash: "synchronize",
userType: 'SAAS_ADMIN',
fullName: 'SAAS Admin',
timeZone: 'pst',
tenantId: 1
}]
).exec(function createCB(err, user) {
logger.info('bootstrap.test.js Create default Service Admin user');
cb();
});
}
},
function(err, results) {
done();
});
});
});
});
after(function (done) {
if (sails && _.isFunction(sails.lower)) {
sails.lower(done);
}
});