我正在使用“ sails-memory”作为我的Sails单元测试的数据库,理想情况下,我希望在每个测试后清除整个数据库。是否有一种方法可以删除整个数据库?
beforeEach((done) => {
"use strict";
// Drops database between each test. This works because we use
// the memory database
sails.once('hook:orm:reloaded', done);
sails.emit('hook:orm:reload');
});
在每次测试之前,您可以提起应用程序的 sails,并重新构建数据库(migrate: 'drop'
)。以下是一个示例:
Sails = require('sails/lib/app');
app = Sails();
var testConfig = {
environment: 'test',
port: 1337,
log: {
level: 'error'
},
connections: {
testDB: {
adapter: 'sails-memory'
}
},
connection: 'testDB',
//wipe/drop ALL my data and rebuild models every time
migrate: 'drop'
};
beforeEach(function (done) {
// start sails app for tests
app.lift(testConfig, function (err, sails) {
done(err);
});
});
//tests...
connection
和 migrate
键放置在配置的 models
对象内才能使其正常工作,像这样:testConfig = { models: { connection: 'testDB', migrate: 'drop' }, ... }
。如果上述方法不起作用,请尝试这个。 - user677526