使用mocha和sinon在节点中模拟http请求 [英] Mocking http requests in node using mocha and sinon

查看:1350
本文介绍了使用mocha和sinon在节点中模拟http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用express编写了一个NodeJS应用程序代理了一些对外部API的调用。所以我正在尝试使用Mocha和Sinon编写单元测试。我的目标是在没有任何互联网连接的情况下测试应用程序,所以我试图模拟https请求并返回模拟回复。

I have written a NodeJS app using express that proxies some calls to external APIs. So I am trying to write a unit test using Mocha and Sinon. My goal is to test the app without any internet connectivity so I am trying to mock the https requests and return mock replies.

我遇到了问题,我可以'找到适合我案例的任何示例或教程。我的节点应用程序在端口8081上侦听http请求,然后将它们代理到另一个站点。我想测试我的应用程序而不必将请求实际发送到那些外部服务器。我在下面尝试它,我把json回复我想要发送回server.respondsWith()函数。

I'm having a problem that I can't find any examples or tutorials that fit my case. My node app listens on port 8081 for http requests and then proxies them to another site. I want to test my app without it having to actually send the request to those external servers. I'm trying it below and I put the json replies I want to send back in the server.respondsWith() function.

我是通过制作正确的方式与柴的ajax通话?或者我应该以某种方式在我的应用程序内发送请求。任何帮助都表示赞赏。

Am I doing this the right way by making an ajax call with chai? or should I be sending the requests inside my app somehow. Any help is appreciated.

var assert = require('assert');
var chai = require('chai');
var spies = require('chai-spies');
var chaiHttp = require('chai-http');
var https = require('https');
var should = chai.should();
var expect = chai.expect;
var sinon = require('sinon');

chai.use(spies);
chai.use(chaiHttp);

describe('Car Repository', function() {
  var server;
  before(function() {
    server = sinon.fakeServer.create();
  });

  after(function() {
    server.restore();
  });

  var url = 'http://127.0.0.1:8081';
  it('should succeed and return a list of cars', function(done) {
    server.respondWith('POST', 'https://api.sandbox.cars.com/v2/token_endpoint', JSON.stringify({"access_token":"1t3E4IykfpJAbuFsdfM2oFAo5raB5vhfOV0hAYe","token_type":"bearer","expires_in":604800}));
    server.respondWith('GET', url+'/cars', JSON.stringify({'test':'this works'}));

    chai.request(url)
      .get('/cars')
      .end(function(err, res) {
        if (err) {
          throw err;
        }

        res.should.have.status(200);
        res.body.should.have.property('test');
        console.log(res.body);

        done();
      });
    });
});


推荐答案

查看 Nock 库。它正是您正在寻找的。

Check out the Nock library. It does exactly what you're looking for.


Nock是Node.js的HTTP模拟和期望库

Nock is an HTTP mocking and expectations library for Node.js

Nock可用于测试单独执行HTTP请求的模块。

Nock can be used to test modules that perform HTTP requests in isolation.

例如,如果模块对CouchDB执行HTTP请求服务器或向Amazon API发出HTTP请求,您可以单独测试该模块。

For instance, if a module performs HTTP requests to a CouchDB server or makes HTTP requests to the Amazon API, you can test that module in isolation.

这篇关于使用mocha和sinon在节点中模拟http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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