我正在尝试使用Node.js和Expresso与Expresso建立一个简单的项目进行测试。我正在使用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端点,以查看是否是导致问题的静态文件,但这给了我同样的错误。

我正在使用Node.js v0.6.6,Express v2.5.4和Expresso v0.9.2。谢谢!

最佳答案

您不是从app文件中导出app.js

将此添加到末尾

module.exports = app;

关于node.js - Expresso测试在导航到静态文件时返回“错误:响应未完成”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8732436/

10-13 07:19