Sinon Fake XML Not Capturing Requests

2019-06-02 06:03发布

问题:

I'm trying to write some tests using Lab and Sinon for various HTTP requests that are called in a file of mine. I followed the Fake XMLHttpRequest example at http://sinonjs.org/ but when I run my tests it appears to not actually capture any requests.

Here is the (relevant) testing code:

context('when provided a valid payload', function () {
    let xhr;
    let requests;

    before(function (done) {
      xhr = sinon.useFakeXMLHttpRequest();
      requests = [];
      xhr.onCreate = function (req) { requests.push(req); };
      done();
    });

    after(function (done) {
      // clean up globals
      xhr.restore();
      done();
    });

    it('responds with the ticket id', (done) => {
      create(internals.validOptions, sinon.spy());
      console.log(requests); // Logs empty array []
      done();
    });
});

create is the function I imported from the other file, here:

internals.create = async function (request, reply) {
  const engineeringTicket = request.payload.type === 'engineering';
  const urgentTicket = request.payload.urgency === 'urgent';

  if (validation.isValid(request.payload)) {
    const attachmentPaths = formatUploads(request.payload.attachments);
    const ticketData = await getTicket(request.payload, attachmentPaths);

    if (engineeringTicket) {
      const issueData = getIssue(request.payload);
      const response = await jira.createIssue(issueData);
      jira.addAttachment(response.id, attachmentPaths);
      if (urgentTicket) {
        const message = slack.getMessage(response);
        slack.postToSlack(message);
      }
    }

    zendesk.submitTicket(ticketData, function (error, statusCode, result) {
      if (!error) {
        reply(result).code(statusCode);
      } else {
        console.log(error);
      }
    });
  } else {
    reply({ errors: validation.errors }).code(400); // wrap in Boom
  }
};

as you can see it calls jira.createIssue and zendesk.submitTicket, both of which use an HTTP request to post some payload to an API. However, after running the test, the requests variable is still empty and seems to have captured no requests. It is definitely not actually submitting the requests as no tickets/issues have been created, what do I need to fix to actually capture the requests?

回答1:

Your problem is apparent from the tags: you are running the code in NodeJS, but the networking stubs in Sinon is for XMLHttpRequest, which is a browser specific API. It does not exist in Node, and as such, the setup will never work.

That means if this should have worked you would have needed to run the tests in a browser. The Karma test runner can help you with this if you need to automate it.

To make this work in Node you can either go for an approach where you try to stub out at a higher level - meaning stubbing the methods of zendesk and jira, or you can continue with the approach of stubbing network responses (which makes the tests a bit more brittle).

To continue stubbing out HTTP calls, you can do this in Node using Nock. Saving the requests like you did above is done like this:

var requests = [];
var scope = nock('http://www.google.com')
 .get('/cat-poems')
 .reply(function(uri, requestBody) {
    requests.push( {uri, requestBody} );
 });

To get some insights on how you can stub out at a higher level, I wrote this answer on using dependency injection and Sinon, while this article by Morgan Roderick gives an intro to link seams.