[Unit Testing] Unit Test a Function that Invokes a Callback with a Sinon Spy
Unit testing functions that invoke callbacks can require a lot of setup code. Using sinon.spy
to create a fake callback function can simplify your unit tests while still allowing you to observe the output of the function you're testing.
const fs = require("fs");
const assert = require("assert");
const sinon = require("sinon");
const jQuery = require("jQuery"); function getTempFiles(callback) {
const contents = fs.readdirSync("/tmp");
return callback(contents);
} describe("getTempFiles", () => {
it("should call the provided callback", () => {
const spy = sinon.spy();
getTempFiles(spy);
assert.equal(spy.callCount, 1);
assert.ok(spy.getCall(0).args[0] instanceof Array);
}); it("should call the function with correct args", () => {
var object = { method: function() {} };
var spy = sinon.spy(object, "method");
object.method(42);
object.method(1);
assert.ok(spy.withArgs(42).calledOnce);
assert.ok(spy.withArgs(1).calledOnce);
}); it("should wrap a existing method", () => {
sinon.spy(jQuery, "ajax");
jQuery.getJSON("/some/resource");
assert.ok(jQuery.ajax.calledOnce);
});
});
[Unit Testing] Unit Test a Function that Invokes a Callback with a Sinon Spy的更多相关文章
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- Unit Testing, Integration Testing and Functional Testing
转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...
- Javascript单元测试Unit Testing之QUnit
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } QUnit是一个基于JQuery的单元测试Uni ...
- [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 ...
- 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 ...
- C# Note36: .NET unit testing framework
It’s usually good practice to have automated unit tests while developing your code. Doing so helps y ...
- [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 ...
随机推荐
- Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级
Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级 // Find components upward function findComponen ...
- docker 创建容器与管理容器
创建容器的选项 [root@mast ~]# docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMM ...
- mysql的sql语句练习的2个网址
sql语句练习: https://blog.csdn.net/mrbcy/article/details/68965271 完成. https://blog.csdn.net/flycat296/ar ...
- Moebius for SQLServer负载均衡
搞数据库的都知道:在Oracle上有RAC集群,MySQL也有对应的方案,而SQL Server上直到SQL Server 2012版本的AlwaysOn到来,微软都没有提供一个负载均衡方案,在网上看 ...
- SQL Server查看表的约束
sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象,包括约束.默认值.日志.规则.存储过程等. SELECT * FROM sysobjects WHERE OBJECT_NAME ...
- 区间DP入门
所为区间DP,主要是把一个大区间拆分成几个小区间,先求小区间的最优值,然后合并起来求大区间的最优值. 区间DP最关键的就是满足最优子结构以及无后效性!! 例如像是石子合并和括号匹配这两类比较经典的模型 ...
- ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)
题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...
- (1) openssl基础概念
1.1 背景知识 对称加密 :加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2. 非对称加密 :使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方 ...
- requests 模块笔记
import requests 请求方式: requests.get("https://www.baidu.com") requests.post("http://htt ...
- C++实现顺序栈类求解中缀表达式的计算
控制台第一行打印的数值为使用形如以下方式得到的结果: cout << +*(+)*/- << endl; 即第一个待求解表达式由C++表达式计算所得结果,以用于与实现得出的结果 ...