gpt4 book ai didi

node.js - Expresso 测试在导航到静态文件时返回 'Error: Response not completed'

转载 作者:太空宇宙 更新时间:2023-11-03 23:46:53 26 4
gpt4 key购买 nike

我正在尝试使用 Node.js 和 Express 与 Expresso 建立一个简单的项目进行测试。我正在使用 express.static 中间件来提供位于我的公共(public)目录中的静态 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 端点,看看是否是静态文件导致了问题,但它给了我同样的错误。

我使用的是 Node.js v0.6.6、Express v2.5.4 和 Expresso v0.9.2。谢谢!

最佳答案

您没有从 app.js 文件导出 app

将其添加到末尾

module.exports = app;

关于node.js - Expresso 测试在导航到静态文件时返回 'Error: Response not completed',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com