快递:npm测试返回“connect ECONNREFUSED”

时间:2014-01-31 22:09:42

标签: node.js unit-testing express compoundjs

我正在尝试测试我的应用,并且它始终返回错误连接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 method)和 / 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();
    });
  });

});

有关如何解决此问题的任何想法? Cross从CompoundJS Google Group发布。

0 个答案:

没有答案