当添加了额外的猫鼬模型.save()时,Promise.all()将失败



当运行4个猫鼬模型保存时,我的Promise.all中出现Mocha错误,但当只运行3个猫鼬模式保存时,这是可以的。下面是description((=>{}下的示例代码。

// 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代码替换为includemyBlog2.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("查找我的博客"(代码运行。

如果数据库离你很远,或者它在一台非常慢的机器上,

你可能真的超过了2秒的超时时间。

你可以通过这样的.timeout(ms(来控制它:

it('mytest', done => {}).timeout(5000)beforeEach().timeout()

将使超时时间变为5秒。

在这里阅读更多关于

希望能有所帮助,祝你好运

最新更新