使用 mocha 对函数进行密集测试,node.js

Intensive testing of a function using mocha , node.js

我正在尝试为我的 node.js 代码编写单元测试。我能够为一个函数编写测试,但我想测试每个网络查询(查询从数据库中获取数据)是否返回所需的响应。 我正在分享我的 node.js 代码以及我的测试代码。

node.js代码(routes.js)

module.exports = {
   getUser: {
        get: function(req, parameters, environment) {

             var id = req.queryString.id;

                  return new Promise(function(resolve, reject) {

                       db.tx(t =>{
                            return t.one("select configuration_value from fint.configuration where configuration_key='LOAN' and application_id =(select application_id from fint.application where application_name='Product Tailoring')")
                       })  
                       .then(conf => {
                            conf_key = conf.configuration_value;
                       }) 

                       db.tx(t =>{
                           return t.one("select smart_account_type_id from fint.smart_account_type where smart_account_type_name='LOAN'")
                       })  
                      .then(acc_type => {
                           smart_acc_type_id =  acc_type.smart_account_type_id;
                      })
                  }
        })
   }
}

这是一个示例代码,我真正想要的是 运行 对单个查询而不是整个函数进行测试。

我的测试代码(test.js)

var expect = require('chai').expect;
var Promise = require('bluebird');
var chai = require('chai'); 
var app = require('../api/smartAccount/identifyLoan/getDirectDebitTrans/routes');

describe("Unit testing for function", function(){
    it("Testing the function using mocha", function(done){
        var req = {
            queryString: {
                uniqueUserId: 101
            }
        };

        var test = app.getUser.get(req);
        return expect(Promise.resolve(test)).to.have.property('_bitField');
        done();
    });
});

任何线索都将不胜感激,如果有人遇到与此相关的链接,请分享。 TIA

首先,如果您想单独测试每个查询,我会重构代码并将每个查询包装在一个单独的函数中。因此,您将能够对真实单元进行单元测试。如果您无法轻松测试应用程序的一部分,可能是因为某些地方的设计方式不正确。顺便说一下,你 return 一个永远不会解决(或拒绝)的承诺!?

关于promise函数的单元测试,你可以试试co-mocha。使用这样的流程控制框架将使您的承诺测试更具可读性。

module.exports = {
  getUser: {
    get: function(req, parameters, environment) {
      var id = req.queryString.id;
      return new Promise(function(resolve, reject) {
        getConfKey(id)
        .then(confKey => {
          getAccType(id)
          .then(accType => {
            resolve({key: confKey, type: accType})
          })
        })
      })
    })

    getConfKey: function(id) {
      return new Promise(function(resolve, reject) {
        db.tx(t =>{
          return t.one("select configuration_value from fint.configuration where configuration_key='LOAN' and application_id =(select application_id from fint.application where application_name='Product Tailoring')")
        })  
        .then(conf => {
          resolve(conf.configuration_value);
        })
      })
    }

    getAccType: function(id) {
      return new Promise(function(resolve, reject) {
        db.tx(t =>{
          return t.one("select smart_account_type_id from fint.smart_account_type where smart_account_type_name='LOAN'")
        })  
        .then(acc_type => {
          resolve(acc_type.smart_account_type_id);
        })
      })
    }
  }
}

当然,这是一个例子。如果这两个功能是独立的,您可以同时执行它们。并发不是本题的重点