[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 ...
随机推荐
- window.onload和DOMContentLoaded的区别
一.何时触发这两个事件? 1.当 onload 事件触发时,页面上所有的DOM,样式表,脚本,图片,flash都已经加载完成了. 2.当 DOMContentLoaded 事件触发时,仅当DOM加载完 ...
- 获取指定点的RGB值
实现效果: 知识运用: Color对象的RGB属性 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFileDia ...
- roi pooling层
roi pooling是先进行roi projection(即映射)然后再池化 映射是把用来训练的图片的roi映射到最后一层特征层(即卷积层).方法其实很简单,图片经过特征提取后,到最后一层卷积层时, ...
- Cycloid Hydraulic Motor Use: Use Failure And Treatment
The cycloidal hydraulic motor is a small low-speed, high-torque hydraulic motor with a shaft-distrib ...
- sublime text 3 安装Nodejs插件
如题 1)集成Nodejs插件到sublime,地址:https://github.com/tanepiper/SublimeText-Nodejs2)解压zip文件, 并重命名文件夹“Nodejs” ...
- TWaver可视化编辑器的前世今生(二)3D编辑器
接着昨天的继续说哈. 作为一款高效.轻量.自带编辑功能小组件,TWaver Java在电信网管界一炮而红,在各大运营商的OSS,BSS,NMS系统中随处可见. 采用了TWaver图形组件的上海世博会监 ...
- [LUOGU] [NOIP2017] P3960 列队
题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n \times mn×m 名学生,方阵的行 ...
- 19. REFERENTIAL_CONSTRAINTS
19. REFERENTIAL_CONSTRAINTS REFERENTIAL_CONSTRAINTS表提供有关外键的信息. REFERENTIAL_CONSTRAINTS有以下列: CONSTRAI ...
- java指令详解
Java是通过java虚拟机来装载和执行编译文件(class文件)的,java虚拟机通过命令java option 来启动,-option为虚拟机参数,通过这些参数可对虚拟机的运行状态进行调整. 一. ...
- 第二讲:vcs debugging basics
要求: 1.describe three methods of debugging verilog code using vcs 2.invoke ucli debugger(不重要) 3.debug ...