简体   繁体   English

使用mocha,node.js进行功能密集测试

[英]Intensive testing of a function using mocha , node.js

i am trying to write a unit test for my node.js code . 我试图为我的node.js代码编写单元测试。 I am able to write the test for a function but i want to test each network query(query fetching data from DB) whether they are returning desired response or not. 我能够为一个函数编写测试,但是我想测试每个网络查询(从DB中获取数据的查询)是否返回期望的响应。 I am sharing my node.js code as well as my test code. 我正在共享我的node.js代码以及测试代码。

node.js code(routes.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;
                      })
                  }
        })
   }
}

This is a sample code, what exactly i want is to run the test on the individual queries instead of the whole function. 这是一个示例代码,我真正想要的是对单个查询而不是整个函数运行测试。

My test code (test.js) 我的测试代码(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();
    });
});

Any leads will be appreciated, and if anybody come across some links regarding the same, please share. 任何线索都将不胜感激,如果有人碰到一些关于同一线索的链接,请分享。 TIA TIA

First of all, if you want to test each query separately, I would refactor the code and wrap each query in a separate function. 首先,如果要分别测试每个查询,我将重构代码并将每个查询包装在单独的函数中。 Thus, you will be able to do unit testing over real units. 因此,您将能够对实际单元进行单元测试。 If you're not able to test a part of your app easily, it's probably because something isn't designed the right way. 如果您无法轻松测试应用程序的一部分,则可能是因为某些东西的设计不正确。 By the way, you return a promise that never resolves (or rejects)!? 顺便说一句,您返回的承诺永远不会解决(或拒绝)!

About the unit testing with promise function, you could give a try to co-mocha . 关于具有promise功能的单元测试,您可以尝试共同购买 Using such a flow control framework will make your promise tests much more readable. 使用这样的流控制框架将使您的承诺测试更具可读性。

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);
        })
      })
    }
  }
}

Of course, this is an example. 当然,这是一个例子。 If both functions are independent, you can execute these concurrently. 如果两个功能都是独立的,则可以同时执行它们。 Concurrency is not the point of this question. 并发不是这个问题的重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM