[Unit Testing] Mock an HTTP request using Nock while unit testing
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [Unit Testing] Node testing: Test api Get request
Using mocha: "devDependencies": { "should": "^5.2.0", "supertest& ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
- Stub, Mock and Proxy Testing
Table of Contents Stubs, Mocks, and Proxies Stub, Mock, and Proxy Testing with Testimonial Mock test ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
- [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 ...
随机推荐
- CAD交互绘制文字(网页版)
在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...
- dos command
dos command md 创建目录 rd 删除目录 cd\ 返回到根目录 cd.. 返回到上一级目录 cd 进入指定目录 dir 列出当前目录下的文件夹及文件 echo 文件内容>文件名称. ...
- 什么是session?
Session一般译作会话.从不同的层面看待session,它有着类似但不全然相同的含义.比如,在web应用的用户看来,他打开浏览器访问一个电子商务网站,登录.并完成购物直到关闭浏览器,这是一个会话. ...
- 微信小程序:errcode=40029和invalid code, hints: [ req_id: VyLhYa0451hb31 ]
问题: 后台用小程序返回的code请求微信服务器换取session_key和openid,返回错误状态码40029 解决问题 当前小程序绑定的appid和请求微信服务器所带的appid参数不一致导致的 ...
- 字典(trie)树--从入门到入土
今天再来认识一个强大的数据结构. 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词 ...
- 1. 调整InnoDB系统表空间的大小
1. 调整InnoDB系统表空间的大小 介绍如何增大或减小InnoDB系统表空间的大小 . 1.1 增加InnoDB系统表空间大小 增加InnoDB系统空间最简单的方法就是,在配置文件中配置autoe ...
- python中的函数的分类
函数的种类 传参的基本要求 默认参数 *args 关键字参数 **kwargs 普通函数 带参数 默认参数 def text(a,b=2) print("haha") print( ...
- finally块的问题(finally block does not complete normally)
http://blog.csdn.net/chh_jiang/article/details/4557461 当finall块中包含return语句时,Eclipse会给出警告“finally blo ...
- pwnable.kr lotto之write up
源代码 : #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcnt ...
- LeetCode03--无重复字符的最长子串
''' 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "ab ...