Node.js 测试快速路线时的数据库计时问题
我正在尝试测试一个express route,它使用在DB中创建一个记录 为了测试这个路由,我使用发出请求,从响应体读取返回的东西ID,然后在数据库中查找该东西以检查它是否存在Node.js 测试快速路线时的数据库计时问题,node.js,express,mocha.js,bookshelf.js,superagent,Node.js,Express,Mocha.js,Bookshelf.js,Superagent,我正在尝试测试一个express route,它使用在DB中创建一个记录 为了测试这个路由,我使用发出请求,从响应体读取返回的东西ID,然后在数据库中查找该东西以检查它是否存在 describe('POST /', function() { it('creates things', function(done) { request(app) .post('/') .send({ name: 'My Thing' }) .
describe('POST /', function() {
it('creates things', function(done) {
request(app)
.post('/')
.send({
name: 'My Thing'
})
.end(function(err, res) {
// res.body.thing exists and has an ID set.
console.log("End occurred", res.body.thing.id);
Thing
.where('id', res.body.thing.id)
.fetch()
.then(function(thing) {
// At this point, thing is null when I would expect to be
console.log("Canvas fetched", thing);
})
});
});
});
在我看来,这就像是一个数据库计时问题,因为它肯定会被创建,至少在响应中返回时它有一个ID。我不知道如何调试它,因为我是NodeJS新手。我甚至没有记录SQL语句
有什么建议吗?我还没有测试过,但你可以试试
new Thing({'id': req.params.id})
.fetch()
.then(function (thins) {
});
基本上,我是在一个beforeach钩子中截断我的数据库,这样每次测试运行都会有一个干净的数据库
beforeEach(function truncateDatabase() {
db.truncate([
'users'
, 'canvases'
, 'module_templates'
])
});
当然,我忘了DB截断是异步的,测试和截断是同时运行的。截断发生在测试中途的某个地方,并破坏了我的设置数据
一旦我将截断挂钩更改为:
beforeEach(function truncateDatabase(done) {
db.truncate([
'users'
, 'canvases'
, 'module_templates'
]).then(done);
});
你用了什么数据库?你确定db有id列吗?对不起,我应该提到。这是PostgreSQL。它肯定有一个ID列,当响应到达时,ID肯定有一个值。
beforeEach(function truncateDatabase(done) {
db.truncate([
'users'
, 'canvases'
, 'module_templates'
]).then(done);
});