我目前正在为我正在与我的团队合作的节点js应用程序编写集成测试。
但是,我似乎无法想象如何继续测试快速服务器。服务器使用redis-server进行缓存。
似乎为了我的测试成功,我应该运行redis-server,并且还单独运行我的应用程序(通过nodemon app),这是没有意义的,因为测试应该独立于任何外部运行命令。
当我在没有运行nodemon应用程序的情况下运行mocha测试时,应用程序永远不会进入glob函数,我得到:
1) Deeplinq Server should return dubai search results via hippie:
Uncaught Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
你可以看看我写的测试
var site = require('../app/lib/site.js');
var hippie = require('hippie');
var http = require('http');
var sinon = require('sinon');
var index = require('../app/index.js');
var server = require('../app/server.js');
var expect = require('chai').expect;
describe('Deeplinq', function () {
describe('Server', function () {
it("should return dubai search results via hippie", function (done) {
hippie(server)
.auth('user', 'user')
.get('http://dsg.deeplinq.dev:3000/en/search?q=dubai')
.expectStatus(200)
.expectHeader('text/html; charset=utf-8')
.end(function (err, res, body) {
if (err) throw err;
done();
})
});
});
});
这是server.js文件的一些部分:
import express from 'express';
import path from 'path';
import hbs from 'express-hbs';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import glob from "glob";
import fs from "fs";
import _ from "lodash";
import Site from './lib/site';
import Vhost from './lib/vhost';
import winston from 'winston';
import expressWinston from 'express-winston';
//require('node-jsx').install({extension: '.jsx'});
require('winston-papertrail').Papertrail;
//import path from "path";
var REDIS_URL = process.env.REDIS_URL || 'redis://127.0.0.1:6379';
var MONGODB_URL = process.env.MONGODB_URL || 'localhost/deeplinq';
var redis = require('redis').createClient(REDIS_URL, {return_buffers: true});
const server = express();
const ENV = ( process.env.NODE_ENV || 'development' );
const PROD = ( ENV==='production' );
var loggerTransports = [];
if (PROD && process.env.PAPERTRAIL_SERVER) {
const trail = process.env.PAPERTRAIL_SERVER.split(':');
loggerTransports.push(new winston.transports.Papertrail({
program: 'deeplinq-ui',
host: trail[0],
port: trail[1],
level: 'debug',
colorize: true,
json: false,
meta: true
}));
}else {
loggerTransports.push(new winston.transports.Console({
json: false,
meta: true,
colorize: true,
level: 'debug'
}));
}
var logger = new winston.Logger({
transports: loggerTransports,
colorize: true,
json: false,
meta: true,
level: 'debug'
});
logger.info('================================================');
logger.info('ENV : %s', ENV);
logger.info('================================================');
// ---------------------------------------------------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------------------------------------------------
server.engine('hbs', hbs.express4({
layoutsDir: __dirname + '/views'
}));
server.set('view engine', 'hbs');
server.set('views', __dirname + '/views');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({extended: true}));
server.use(expressWinston.logger({
transports: loggerTransports,
level: 'debug',
meta: false, // optional: control whether you want to log the meta data about the request (default to true)
//msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: false, // Use the default Express/morgan request formatting, with the same colors. Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true
colorStatus: true, // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true
ignoreRoute: function (req, res) { return req.url.match(/(css|js|gz|png|gif|jpeg|jpg|woff|wotf|ttf|ico)/); } // optional: allows to skip some log messages based on request and/or response
}));
// ---------------------------------------------------------------------------------------------------------------------
// Virtual hosts management
// ---------------------------------------------------------------------------------------------------------------------
var vhost = new Vhost();
// ---------------------------------------------------------------------------------------------------------------------
// Load sites
// ---------------------------------------------------------------------------------------------------------------------
redis.on("connect", function () {
// ---------------------------------------------------------------------------------------------------------------------
// Bootstrap
// ---------------------------------------------------------------------------------------------------------------------
server.use(vhost.vhost(server.enabled('trust proxy')));
// Scan sites definitions (dl.json)
glob("sites/**/dl.json", {}, function (er, files) {
logger.info("= GLOB ===============================================");
_.each(files, function (file) {
var dl = require(path.resolve(file));
var _env = dl.env;
var props = require(path.resolve(path.join(path.dirname(file), 'views', 'props')));
var env = _env.default;
_.merge(env, _env[ENV]);
logger.info('Registering: %s / %s', dl.id, dl.about);
env.id = dl.id;
env.REDIS_URL = REDIS_URL;
_.each(env.dns, function (dns) {
var context = dns.split('/');
if (context.length>1) {
context = '/' + context[1]
}else {
context = '/'
}
// TODO:ENHANCE: Create one Site per dl.json not per dns entry
var site = new Site(context, env, props, path.dirname(file), redis, logger);
vhost.register(dns, site.app);
logger.info('Host registed: %s', dns);
console.log("")
})
});
server.use(expressWinston.errorLogger({transports: loggerTransports}));
const port = process.env.PORT || 3000;
server.listen(port);
logger.info("Server's listening on port %s", port);
logger.info("================================================");
});
});
export default server;
有人能指出我正确的方向吗?我做错了什么,我该怎么做才能解决这些问题?