アプリをテストしようとしていますが、常にconnect ECONNREFUSEDのエラーが返されます。何が起こっているかを示す簡単な例を作成しました。これが私のコントローラーです(CompoundJSコード):
load('application');
action('test', function() {
var obj = {success: true, data: 'blah'};
send(obj);
});
action(function show(data) {
var http = require('http');
var options = {
path: '/getTest',
port: process.env.PORT // without this, http fails because default port is 80
};
var req = http.get(options, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
return send(data);
});
});
req.on('error', function(e) {
return send({success: false, data: e.message}); // returns "connect ECONNREFUSED"
});
});
そのため、アプリを実行しているときに、/test (そこにある show メソッド) と/getTestをヒットしても、エラーは発生しません。ただし、次のテスト コードを実行しようとすると、上記のエラーが発生し、show 関数に問題なくアクセスできるため、問題はその http.get に帰着します。
var app, compound
, request = require('supertest')
, sinon = require('sinon');
function TestStub() {
return {
};
}
describe('TestController', function() {
beforeEach(function(done) {
app = getApp();
compound = app.compound;
compound.on('ready', function() {
done();
});
});
/*
* GET /tests
* Should render tests/index.ejs
*/
it('should render "index" template on GET /tests', function(done) {
request(app)
.get('/test')
.end(function(err, res) {
console.log(res.body);
done();
});
});
});
これを修正する方法についてのアイデアはありますか? CompoundJS Google Groupからクロス投稿されました。