mercredi 23 août 2017

How to unit test http wrapper in node.js? [duplicate]

This question already has an answer here:

I am writing an http wrapper around a third party dependency and struggling with a way to test it.

Here is an example which returns a promise:

const tinyreq = require('tiny-req')

class Test {

  makeRequest (method, url, body) {
    return tinyreq({
      method,
      url,
      body
    })
  }

}

What is a better way to test makeRequest?

  1. To actually call this method using a method, url and body such as GET, http://example.com and {}
  2. To create a stub for this function while resolving (with response body as success value) and rejecting (with error object as failure value) it subsequently in multiple tests.

P.S. I am using chai, mocha, sinon and chai-as-promised.

Aucun commentaire:

Enregistrer un commentaire