2017-10-11 57 views
0

我是NodeJS/Express的新手,我有這個問題與全局/模塊共享模塊和配置設置有關,我將在這裏解釋。Express API上的模塊共享和動態屬性

一個簡單的例子就是試圖對包含其他庫的中間件模塊進行單元測試。如下:

配置/ settings.js

module.exports = { 
..., 
debugLevel: 3 

};

中間件/ auth.js

// Required modules for middleware 
// ---------------------------------------------------------------------- 
var config = require('../config/settings'); 

var log = require('../libraries/logger')('middlewares/auth'); 
... 

// Middleware methods 
// ---------------------------------------------------------------------- 
var middleware = {}; 

middleware.apicall = function(req, res, next) { 
    ... 
    log.info('Made api call'); 
}; 

庫/ logger.js

// External Dependencies 
var winston = require('winston'); 
var winlogger = new (winston.Logger)({ 
    transports: [ 
     new (winston.transports.Console)() 
    ] 
}); 
winlogger.cli(); 


var logger = function(module, debug) { 
    this.module = module; 
    this.logger = winlogger; 
    this.debugLevel = debugLevel; 
}; 

logger.prototype = { 
    module: '', 
    logger: null, 
    debugLevel: 3, 


    /** 
    * Generic Log function 
    * 
    * @param level 
    * @param str 
    * @param data 
    */ 
    debug: function(level, str, data) { 
     if (this.debugLevel > 2) this.logger.log(level, this.module+' - '+str, data); 
    }, 

    /** 
    * Specific function for info 
    * 
    * @param str 
    * @param data 
    */ 
    info: function(str, data) { 
     if (this.debugLevel > 1) this.logger.info(this.module+' - '+str, data); 
    } 
}; 

測試/ api.js

const chai = require('chai'); 
const should = chai.should; 
const expect = chai.expect; 
const assert = chai.assert; 

// Node Http Mocks 
var httpMocks = require('node-mocks-http'); 
var middleware = require('../middlewares/auth'); 

describe("Test API Call", function() { 

    it("should give 200 over api call", function(done) { 
     var req = httpMocks.createRequest({ 
      method: 'GET', 
      url: '/test', 
      params: {} 
     }); 
     var res = httpMocks.createResponse(); 
     middleware.apicall(req, res, function(res) { return res; }); 

     assert.equal(res.statusCode, 200); 

     done(); 
    }); 

}); 

現在所有這些工作,但我遇到的問題是:我怎麼能做的測試沒有從apicall方法顯示的日誌行?

有沒有辦法讓設置全球化?還是構建應用程序的一種方式,以便您可以在app.js中注入來自單個需求的設置?

我需要做的,在測試/ api.js,這樣的事情:

var config = require('../config/settings'); 
config.debug = 0; 

,注入測試可能。任何人都面臨類似的挑戰?

回答

0

ANSWER

已經找不到解決辦法,我試圖讓一系列的測試,並與一個可行的解決方案上來。

我們在這個特殊情況下使用全局變量。所以:

斌\ WWW

#!/usr/bin/env node 

/** 
* Module dependencies. 
*/ 
var app = require('../app'); 
... 
var http = require('http'); 

global.config = require('../config/settings'); 
global.log = require('../libraries/logger')('server'); 

... 

app.js

// Node framework dependencies 
// ---------------------------------------------------------------------- 
var express = require('express'); 
var config = global.config; 
var path = require('path'); 
var log = global.log; 
log.setLabel('app'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var cors = require('cors'); 
var validator = require('express-validator'); 
... 

測試\ api.js

const chai = require('chai'); 
const should = chai.should; 
const expect = chai.expect; 
const assert = chai.assert; 

global.config = require('../config/settings'); 
config.debug = false; 
global.log = require('../libraries/logger')('Test case'); 

... 
// Node Http Mocks 
var httpMocks = require('node-mocks-http'); 
var middleware = require('../middlewares/auth'); 


describe("Test API Call", function() { 
    it("should give 200 over api call", function(done) { 
     var req = httpMocks.createRequest({ 
      method: 'GET', 
      url: '/test', 
      params: {} 
     }); 
     var res = httpMocks.createResponse(); 
     middleware.apicall(req, res, function(res) { return res; }); 
     assert.equal(res.statusCode, 200); 
     done(); 
    }); 
});