[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 you would like to observe, or at the very least mock to prevent side effects like network activity or file system operations.
For JavaScript unit tests that run in Node, we can hijack the built-in require
function by using the proxyquire
module, which allows us to mock one or more modules that are second-class dependencies in our unit test, replacing them with whatever we want.
This means we could replace functions with no-ops to prevent side effects, or replace them with Sinon spies to observe the inner workings of the module you're testing.
In this video, we'll demonstrate the basics of using Proxyquire to prevent and observe a file system operation.
index.js:
const fs = require('fs'); exports.writeFile = (filename, contents) => {
fs.writeFileSync(filename, contents.trim(), 'utf8');
}
For unit testing, we don't want to call 'fs.writeFileSync', instead we want to using mock function.
const assert = require('assert');
const proxyquire = require('proxyquire');
const sinon = require('sinon'); const fsMock = {};
// In index.js file, we want to mock 'fs' module with our fsMock object
const main = proxyquire('./index.js', {'fs', fsMock});
describe('main.writeFile', () => {
it('should write a trimmed string to the specififed file', () => {
fsMock.writeFileSync = sinon.spy();
main.writeFile('file.txt', 'string');
assert.deepEqual(fsMock.writeFileSync.getCall(0).args, ['file.txt', 'string', 'utf8'])
})
})
[Unit Testing] Mock a Node module's dependencies using Proxyquire的更多相关文章
- [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 an HTTP request using Nock while unit testing
When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...
- [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
- Unit Testing PowerShell Code with Pester
Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
随机推荐
- laravel的elixir和gulp用来对前端施工
使用laravel elixer npm install --global gulp ok 然后在安装好的laravel 下 npm install 以安装 laravel-elixir subli ...
- 汕头市队赛 SRM 08 C
C-3 SRM 08 描述 给一个图,n 个点 m 条双向边,每条边有其长度.n 个点中有 k 个是特殊点,问任意两个特殊点的最短路是多少. 输入格式 第一行三个整数 n m k 第二行 k 个整数 ...
- C#操作XML序列化与反序列化
public class XmlSerializerHelper { /// <summary> /// 从XML文件中反序列化读取对象 /// </summary> /// ...
- [译] 如何像 Python 高手一样编程?
转自:http://www.liuhaihua.cn/archives/23475.html Harries 发布于 7天前 分类:编程技术 阅读(15) 评论(0) 最近在网上看到一篇介绍Pytho ...
- 嵌入式Linux上通过boa服务器实现cgi/html的web上网【转】
转自:http://blog.csdn.net/tianmohust/article/details/6595996 版权声明:本文为博主原创文章,未经博主允许不得转载. 嵌入式Linux上通过boa ...
- Linux中tty框架与uart框架之间的调用关系剖析【转】
转自:http://developer.51cto.com/art/201209/357501.htm 之前本人在"从串口驱动的移植看linux2.6内核中的驱动模型 platform de ...
- 非常好!!!Linux源代码阅读——内核引导【转】
Linux源代码阅读——内核引导 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/1_boot.html 目录 Linux 引导过程综述 BI ...
- C# split字符串
string strSourse = "ab|||cdef"; string[] arr = strSource.Split(new string[]{"|||" ...
- linux安装mongodb(设置非root用户和开机启动)
官网地址:https://www.mongodb.com/ 在官网上选择不同的linux系统得到不同的下载地址,我们用的下载地址是:https://fastdl.mongodb.org/linux/m ...
- Python的并发并行[1] -> 线程[2] -> 锁与信号量
锁与信号量 目录 添加线程锁 锁的本质 互斥锁与可重入锁 死锁的产生 锁的上下文管理 信号量与有界信号量 1 添加线程锁 由于多线程对资源的抢占顺序不同,可能会产生冲突,通过添加线程锁来对共有资源进行 ...