【问题标题】:Jest - is it possible to test specific variables within an async module?开玩笑 - 是否可以在异步模块中测试特定变量?
【发布时间】:2018-07-06 05:22:18
【问题描述】:

我想知道是否可以在模块调用中测试特定值的变量。例如:

module.exports = async(request, context, postgres) => {

   let userId = request.user.id;
   let balanceQuery = 'SELECT balance FROM account WHERE user_id = $1';
   let userBalanceStr = await postgres.query(balanceQuery,[userId]).then(result => result.rows[0].balance);
   let totalCount = BigFunction(userBalanceStr);
   ......
}

我将如何在开玩笑测试中测试/访问 balanceQueryuserBalanceStr 的值?

我只能访问查询的userId 部分,我有点困惑如何访问模块中的其他变量。

【问题讨论】:

    标签: javascript node.js postgresql jestjs


    【解决方案1】:

    您可以通过模拟 postgres.queryBigFunction 然后断言每次调用中传递的参数来实现这一点。为了能够测试异步代码,您可以实现异步test
    API参考:Mock Functions
    在模拟postgres.query 时,您可以实现一个虚假的返回值,该值将分配给userBalanceStr,然后可以使用模拟的BigFunction 进行断言。 要模拟BigFunction,您可以模拟其在被测模块中存在的importation,或者更改导出的函数以将BigFunction 作为依赖项传递,如postgres,直接将模拟函数作为参数传递。

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2019-04-18
      • 1970-01-01
      • 2019-06-30
      • 2021-10-12
      • 2020-10-08
      • 1970-01-01
      • 2019-05-17
      • 2018-11-23
      相关资源
      最近更新 更多