samedi 2 janvier 2016

Mocha test suite errorring out when attempting to connect to API

I'm running my test suite using mocha, via gulp-jsx-coverage and gulp-mocha. All my tests run and pass/fail as expected. However, some of my modules being tested make HTTP requests to my API via the superagent library.

When in development, I'm also running my API at localhost:3000 alongside my client-side app, and so that is the URL my client-side tests are attempting to access. When testing, though, the API is usually not running. This results in the following error any time a request gets through:

Error in plugin 'gulp-mocha'
Message:
    connect ECONNREFUSED
Details:
    code: ECONNREFUSED
    errno: ECONNREFUSED
    syscall: connect
    domainEmitter: [object Object]
    domain: [object Object]
    domainThrown: false
Stack:
Error: connect ECONNREFUSED
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:983:19)

I've tried stubbing all of the methods on the superagent (aliased as request) library in a global helper, like so:

beforeEach(function() {
  global.sandbox = sinon.sandbox.create();

  global.getStub = global.sandbox.stub(request, 'get', httpStub);
  global.putStub = global.sandbox.stub(request, 'put', httpStub);
  global.patchStub = global.sandbox.stub(request, 'patch', httpStub);
  global.postStub = global.sandbox.stub(request, 'post', httpStub);
  global.delStub = global.sandbox.stub(request, 'del', httpStub);
});

afterEach(function() {
  global.sandbox.restore();
});

But for some reason, when some tests are encountered the methods aren't stubbed and so I reach the ECONNREFUSED error. I've triple checked, and no where am I restoring the sandbox or any stubs.

Is there either a way to fix the problem I'm running into, or a cleaner solution for this overall?

Aucun commentaire:

Enregistrer un commentaire