2016-07-13 57 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

其實它更多的是我想測試函數內部的函數,即我想寫我的測試用例,因爲它測試了寫入測試用例的根函數內部的多個函數。你能提供一點知識嗎? –

相關問題