我正在尝试设置一个简单的项目,使用Node.js和Express与Express进行测试。我正在使用express.static中间件提供位于我的公共目录中的静态html文件(index.html)。这是app.js的代码:
var express = require('express');
var app = express.createServer();
// Configuration
app.configure(function() {
app.use(express.staticCache());
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
});
app.listen(3000);然后,我编写了一个简单的测试来检查静态文件。这是app.test.js的代码:
var app = require('../app')
, assert = require('assert');
module.exports = {
'Navigate to root': function() {
assert.response(
app,
{
url: '/'
},
{
status: 200,
headers: { 'Content-Type': 'text/html; charset=utf8'}
},
function(res) {
assert.includes(res.body, 'index');
assert.ok(res);
}
);
}
};当我运行测试(使用expresso或expresso -s)时,我得到以下错误:
app.test.js Navigate to root: TypeError: Object #<Object> has no method 'listen'
at Function.response (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:424:16)
at Test.fn (/home/bill/projects/bidkat.app/test/app.test.js:13:11)
at Test.runParallel (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:959:10)
at Test.run (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:924:18)
at next (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:867:22)
at runSuite (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:875:6)
at check (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:814:12)
at runFile (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:819:6)
at Array.forEach (native)
at runFiles (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:796:15)
app.test.js Navigate to root: Error: Response not completed: Navigate to root.
at Function.response (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:400:17)
at Test.fn (/home/bill/projects/bidkat.app/test/app.test.js:13:11)
at Test.runParallel (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:959:10)
at Test.run (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:924:18)
at next (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:867:22)
at runSuite (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:875:6)
at check (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:814:12)
at runFile (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:819:6)
at Array.forEach (native)
at runFiles (/home/bill/local/node/lib/node_modules/expresso/bin/expresso:796:15)我显然在做一些愚蠢的事情,但我似乎不知道它是什么。我想这可能是因为我的服务器在测试之前已经启动了,但看起来Expresso可以处理这种情况。我还使用json测试了一个REST端点,以查看是否是静态文件导致了问题,但它给出了相同的错误。
我使用的是Expresso0.6.6、Node.js 2.5.4和Expresso0.9.2。谢谢!
发布于 2012-01-05 04:24:11
您没有从app.js文件中导出app
把这个加到最后
module.exports = app;https://stackoverflow.com/questions/8732436
复制相似问题