2016-07-13 59 views
0

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

回答

1

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

关于使用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); 
     }) 
     }) 
    } 
    } 
} 

当然,这是一个例子。如果两个函数都是独立的,则可以同时执行这些函数。并发不是这个问题的关键。

+0

谢谢,它有帮助。 诺言解决了也拒绝,但我没有把这部分放在代码中,因为我主要关心的是在单个函数中测试多个网络查询。 只是出于好奇,是否有可能在单个函数中测试多个查询? –

+0

你可以测试任何你想要的。给一个输入,测试输出。按照定义,单元测试是测试一个单元,这样做会使测试简单明了。为什么要在单个函数中测试多个查询? – Cyril

+0

其实它更多的是我想测试函数内部的函数,即我想写我的测试用例,因为它测试了写入测试用例的根函数内部的多个函数。你能提供一点知识吗? –

相关问题