0

我正在尝试测试我的应用程序,它总是返回连接 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就可以了,没有任何错误。但是,当我尝试运行以下测试代码时,我得到了上述错误,问题归结为那个 http.get,因为我可以很好地进入 show 函数。

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的 Cross 发布。

4

0 回答 0