2014-10-01 100 views
0

我已经正确配置了我的应用程序,例如,如果我运行node app.js,所有内容都将启动。我试图测试我的应用程序的连接正在使用单元测试正常工作。我至今是:如何使用express,nodeJS和Mocha测试应用程序连接

var options = { 
    url: 'http://localhost', 
    port: 8080, 
}; 
var app = require("../app.js"); 
var should = require('should'); 
var assert = require("assert"); 
var async = require('async'); 

it ('tests the ok connection of the app by checking that it is listening on port 8080', function(dont) { 
    request(options,app) 
    .get('/') 
    .expect(200) 
    .end(function (err, res) { 
    res.header['location'].should.include('/') 
    res.text.should.include('Listening on port 8080'); 
    done(); 
    }); 
}); 

我收到错误消息Error: options.uri is a required argument,这就是为什么我添加了var options。现在我收到错误消息TypeError: Object #<Request> has no method 'get'

如何正确检查我是否有良好的连接(200),我在正确的页面('/'),并且在连接时记录消息Listening on port 8080

谢谢!

+0

你只需要使用'request(app)'而不用选项。另外'应该'没有'.include',你需要使用'.containEql'。 – 2014-10-02 07:30:07

回答

0

对于TypeError,请确保您使用supertest而不是其他类似request。原因是模块request不提供.expect()和其他功能。

+1

谨慎解释downvote? – mscdex 2014-10-02 12:13:07

+0

感谢您的回复 - 当我访问''/''页面时,我希望它显示我发送的html。因此,我不知道是否对我使用'app.get('/ user',function(req,res){res.send(200);});'这样的东西是好的,因为它只是显示一些代替我想要显示的html的东西。那么,我如何测试着陆页上的任何内容? – maudulus 2014-10-02 13:48:52