Sinon Fake XML无法捕获请求 [英] Sinon Fake XML Not Capturing Requests

查看:107
本文介绍了Sinon Fake XML无法捕获请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Lab 和Sinon编写一些测试,以测试各种HTTP请求在我的档案中.我在 http://sinonjs.org/上遵循了Fake XMLHttpRequest示例,但是当我运行测试时,它似乎没有实际捕获任何请求.

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是我从另一个文件导入的功能,在这里:

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
  }
};

如您所见,它调用了jira.createIssue和zendesk.submitTicket,它们都使用HTTP请求将一些有效载荷发布到API.但是,运行测试后,requests变量仍然为空,并且似乎未捕获任何请求.绝对没有提交请求,因为尚未创建票证/问题,我需要解决什么才能真正捕获请求?

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?

推荐答案

从标记中可以明显看出您的问题:您正在NodeJS中运行代码,但是Sinon中的网络存根是针对XMLHttpRequest的,这是特定于浏览器的API.它在Node中不存在,因此设置将永远无法进行.

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.

这意味着,如果应该这样做,您将需要在浏览器中运行测试. Karma测试运行程序可以在需要自动化的情况下为您提供帮助.

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.

要使此功能在Node中起作用,您可以采用一种尝试在更高级别上进行存根的方法-意味着对zendeskjira的方法进行存根,或者可以继续对网络进行存根的方法响应(这会使测试更加脆弱).

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).

要继续进行HTTP调用,您可以在Node中使用 Nock 进行.像上面所做的那样保存请求是这样完成的:

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} );
 });

为了获得有关如何进行更高级别存根的一些见解,我写了此答案有关使用依赖项注入的信息和Sinon,而摩根·罗德里克(Morgan Roderick)的这篇文章介绍了链接接缝

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.

这篇关于Sinon Fake XML无法捕获请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆