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. CPP-STL:list容器

    本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通函数,而且例子丰富,通俗易懂.不失为STL的入门文章,新手不容错过! 目录 1 定义一个list 2 使用list的成员函数p ...

  2. Caused by: java.lang.ClassNotFoundException: Cannot find class: User

    源代码: <select id="selectAll" resultType="User"> select user_id uid,user_nam ...

  3. 简单批处理命令直接启动你的AVD

    大家都知道,要想启动AVD,一般方法是先打开Android SDK and AVDmanager,再选择你要启动的AVD选择start(废话) 那么,有没有一种简单的方法在任何位置一键启动你指定的av ...

  4. react笔记汇总

    1.什么是React? a.React 是一个用于构建用户界面的 JAVASCRIPT 库. b.React主要用于构建UI,很多人认为 React 是 MVC 中的 V. c.React 起源于 F ...

  5. hdu5279 YJC plays Minecraft

    题目描述 题解: 岛屿之间的边砍/不砍情况有$2^n$种, 但是需要剪掉所有的岛上都首尾相连的情况. $dp$一下对于完全图没有限制($f$)/有限制($g$)的情况数. 方程:$$f[i]=\sum ...

  6. logging日志模块配置

    logging日志模块 日志级别 日志一共分成5个等级,从低到高分别是: 1)DEBUG 2)INFO 3)WARNING 4)ERROR 5)CRITICAL 说明: DEBUG:详细的信息,通常只 ...

  7. solr中的Tokenizer Filter

    Tokenizer Tokenizer 的工作是将文本流分解为令牌,其中每个令牌(通常)是文本中字符的子序列.分析器知道它配置的字段,但 tokenizer 不是.Tokenizers 从字符流(Re ...

  8. SQLServer__问题记录

    “备份集中的数据库备份与现有的xx数据库不同”  参考链接: http://www.cnblogs.com/huangfr/archive/2012/08/09/2629687.html RESTOR ...

  9. tcpcopy简单用法

    这篇文章介绍下网易开源的流量重放(replay)工具TCPCopy,说是简单介绍,绝对不是谦虚,因为自己了解的确实也不多.为什么不甚了解呢,大家可以到TCPCopy的官方仓库看看,https://gi ...

  10. angularjs ng-repeat下验证问题

    angularjs验证要求name唯一,repeat情况,name 等通过${index}等绑定,也无法获取值 通过ng-from的方法,这样验证name重复也可以了. <ng-form nam ...