When testing functions that make HTTP requests, it's not preferable for those requests to actually run. Using the nock JavaScript library, we can mock out HTTP requests so that they don't actually happen, control the responses from those requests, and assert when requests are made.

const assert = require('assert');
const nock = require('nock');
require('isomorphic-fetch'); function getData() {
return fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json());
} describe('getData', () => {
it('should fetch data', () => {
const request = nock('https://jsonplaceholder.typicode.com')
.get('/users')
.reply(200, [{username: 'joe'}]); getData()
.then(response => {
assert.deepEqual(response, [{username: 'joe'}]);
assert.ok(request.isDone());
});
});
})

[Unit Testing] Mock an HTTP request using Nock while unit testing的更多相关文章

  1. [Redux-Observable && Unit Testing] Mocking an ajax request when testing epics

    Often in unit tests we are focussing on the logic involved in crafting a network request, & how ...

  2. [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests

    In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...

  3. [Unit Testing] Mock a Node module's dependencies using Proxyquire

    Sometimes when writing a unit test, you know that the module you're testing imports a module that yo ...

  4. [Unit Testing] Node testing: Test api Get request

    Using mocha: "devDependencies": { "should": "^5.2.0", "supertest& ...

  5. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  6. Stub, Mock and Proxy Testing

    Table of Contents Stubs, Mocks, and Proxies Stub, Mock, and Proxy Testing with Testimonial Mock test ...

  7. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  8. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  9. [Unit Testing] AngularJS Unit Testing - Karma

    Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...

随机推荐

  1. SSM框架删除/更新时返回影响条数

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">    < ...

  2. C和C++中动态链接库的创建和链接(原创,装载请注明原处)

    C和C++中动态链接库的创建和链接 1.创建DLL(动态链接库)-C++方式 1.创建DLL(动态链接库-C++方式) 1.在VS(以VS2017为例)中创建DLL动态链接库. 解决方案名称为:MyD ...

  3. 如何用纯 CSS 创作一个荧光脉冲 loader 特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/erRzzR 可交互视频教程 此视 ...

  4. 一道在CF上WA了9次才AC的A题题目与10个版本的代码代码

    题目(题目链接:https://codeforces.com/problemset/problem/733/A):   A. Grasshopper And the String time limit ...

  5. Java学习之正则表达式

    Java正则表达式字符串模式. 正则表达式可以用来搜索.编辑和处理文本. 正则表达式不尽限于一种语言,但在每一种语言中又细微的差别. java.util.regex包中主要有这3个类: Pattern ...

  6. luogu3313 [SDOI2014]旅行

    对每一个宗教建一棵线段树,然后树剖搞搞 #include <iostream> #include <cstdio> using namespace std; int n, m, ...

  7. STM32F407 NVIC 中断优先级管理 个人笔记

    内嵌向量中断控制器:Nested Vectored Interrupt Controller (NVIC) 设置中断向量的优先级并使能. 响应优先级& 抢占优先级 抢占优先级:一个中断A还在处 ...

  8. 大数据学习——hadoop安装

    上传centOS6.7-hadoop-2.6.4.tar.gz 解压 tar -zxvf centOS6.7-hadoop-2.6.4.tar.gz hadoop相关修改配置 1 修改 /root/a ...

  9. ssm依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. php5.5编译安装

    系统环境:centos6.5PHP包:5.5.15https://wiki.swoole.com/wiki/page/177.html下载 PHP 源码包wget http://cn2.php.net ...