【发布时间】:2019-05-03 17:27:46
【问题描述】:
运行 4 次猫鼬模型保存时,我的 Promise.all 中出现 Mocha 错误,但仅运行 3 次猫鼬模型保存时没问题。这是 describe () => {} 下的示例代码。
// Blog and Tag are mongoose models.
beforeEach((done) => {
myBlog = new Blog({ title: '1st Post' });
myBlog2 = new Blog({ title: '2nd Post' });
tag1 = new Tag({ tag: 'Tag 1' });
tag2 = new Tag({ tag: 'Tag 2' });
myBlog.tag.push(tag1, tag2);
myBlog2.tag.push(tag1);
tag1.blog.push(myBlog, myBlog2);
tag2.blog.push(myBlog);
Promise.all([myBlog.save(), tag1.save(), tag2.save()])
.then(() => done());
});
it('find myBlog', (done) => {
Blog.findOne({ title: '1st Post'})
.then((blog) => {
assert(blog.title === '1st Post');
done();
});
});
以上代码运行良好,但是当我将初始 Promise.all 代码替换为包含 myBlog2.save() 时,出现错误。
Promise.all([myBlog.save(), myBlog2.save(), tag1.save(), tag2.save()])
.then(() => done());
"before each" hook for "find myBlog":
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
为什么在 Promise.all() 中添加额外的 model.save() 会导致错误?
我检查了 mongodb,集合确实保存成功。但是,我根本不相信 it('find myBlog') 代码会运行。
【问题讨论】:
标签: mongodb mongoose mongodb-query mocha.js