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. 1-jdk的安装与配置

    1- Jvm.jdk.jre之间的关系 JVM:Java虚拟机,保证java程序跨平台.(Java Virtual Machine) JRE: Java运行环境,包含JVM和核心类库.如果只是想运行j ...

  2. html前端如何将一个页面表单内的数据全部传递到另一个页面?

    http://blog.csdn.net/stone_tomcate/article/details/64148648?winzoom=1

  3. python书籍推荐:量化投资:以Python为工具

    所属网站分类: 资源下载 > python电子书 作者:mimi 链接:http://www.pythonheidong.com/blog/article/451/ 来源:python黑洞网 内 ...

  4. python 以及 pywin32添加注册表

    python 添加注册表信息: import sys from winreg import * # tweak as necessary version = sys.version[:3] insta ...

  5. 剑指Offer(书):实现单例模式

    题目:设计一个类,我们只能生成该类的一个实例. 生成一个单例模式,有下列几种常用解法. 1.双重检查锁. 当运行在多线程环境中,此方法可以正常实现单例模式. public class Singleto ...

  6. HDU 1242 Rescue 营救天使

    Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...

  7. POJ 2976 Dropping test(01分数规划模板)

    01分数划分详情可阅读:http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html 题意: 给出n个a和b,让选出n-k个使得最大 二 ...

  8. Eclipse设置反编译插件

    有些项目我们想看看引入的包的源码的时候,因为打包好的.class文件的内容我们是看不懂的,但是又懒得去找源码文件的时候,就会用到反编译工具. 步骤: 1.安装反编译插件. 2.设置使用的反编译工具. ...

  9. 大数据学习——mapreduce倒排索引

    数据 a.txt hello jerry hello tom b.txt allen tom allen jerry allen hello c.txt hello jerry hello tom 1 ...

  10. Python MySQLdb 查询中文出现问号的解决方法

    在连接数据库的时候设置如下即可: db = MySQLdb.connect('localhost','root','×××××','test',use_unicode=True,charset=&qu ...