0

介绍

我有一个简单的方法从我的猫鼬数据库中获取 X 行。我正在尝试使用 Mocha 测试此方法。

这是方法:

public async findManyByCount (amount: number) {
  const profiles: any = await Document.find({}).sort({'date': -1}).limit(amount)
  return profiles
}

这是我的测试:

describe.only('`findManyByCount`', function () {
  this.timeout(10000)
  it('Should return the number of profiles specified', async function () {
    const Profile = new ProfileModel
    const count = 5
    const profiles = await Profile.findManyByCount(count)
    expect(profiles.length).to.equal(count)   
  })
})

问题

运行测试时出现以下错误:

Profile Model
  Methods
    `findManyByCount`
      1) Should return the number of profiles specified
      - Should fail if no count is specified


  0 passing (10s)
  1 pending
  1 failing

  1) Profile Model
       Methods
         `findManyByCount`
           Should return the number of profiles specified:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/var/www/api/tests/models/ProfileModel.js)

我之前已经解决了这种类型的错误消息,但无论我做什么,我似乎都无法修复它。

我还应该注意,我可以在 shell 中很好地运行 mongoose 查询,它在我的网站中也可以正常运行。我还应该确认查询是什么挂起 -await ....运行后什么都没有

我尝试过的事情

我试过增加/减少超时

将查询简化为.find({})

尝试/捕获块

问题

什么会导致这个问题?就像我说的,我的 web 应用程序运行良好的查询(调用登录页面上的方法),我可以在 shell 中运行查询并获得即时结果

编辑

我发现,运行 mocha 测试时的许多查询都是这种情况。我肯定也连接到数据库 - 我不知道如何再调试它以找出问题?

4

0 回答 0