【问题标题】:how do I test asynchronous calls in mongoose with expresso如何使用 espresso 在猫鼬中测试异步调用
【发布时间】:2012-01-03 21:21:48
【问题描述】:

我一直在尝试学习如何使用 expresso 进行单元测试,但对于从异步 mongoose 数据库调用中获取结果感到困惑。似乎测试没有等待异步调用返回,所以我的断言总是失败。

"get tasks" : (beforeExit, assert) ->
    tasks = null
    Task.find {}, (err, result) ->
        if not err
            console.log 'results'
            tasks = result
        else
            console.log 'error' + err   
        should.not(err)

    beforeExit ->
        tasks.should.not(null)

它似乎也可能挂在 mongoose connect 调用上

db = mongoose.connect config.connStr

【问题讨论】:

    标签: unit-testing mongoose


    【解决方案1】:

    所以想出了导致这件事发生的几件事。我的第一个错误是在拆卸/退出前没有调用 mongoose.disconnect(),我应该在设置函数中调用 mongoose.connect。我遇到的另一个问题是,如果我需要对不在同一父目录中的模型文件的引用,我的测试无法从 mongoose 模型查询 Task.find {} 中获得响应。

    因此将测试文件保留在根父目录会导致 Task.find {} 挂起,即使引用正确

    models = require "./../Services/models"
    

    但如果我将测试文件上移到服务文件夹中,它就神秘地工作了,不知道为什么

    models = require "./../models"
    

    【讨论】:

      猜你喜欢
      • 2017-02-22
      • 2021-03-01
      • 2018-02-27
      • 2014-10-12
      • 1970-01-01
      • 2020-11-01
      • 2015-11-03
      • 2014-11-16
      • 2019-10-12
      相关资源
      最近更新 更多