[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 组件名和方法名 重名了,报function错误
vue 组件名和方法名 重名了,报function错误
- Linux内核学习总览
断断续续学习操作系统已经有大半年时间了,一直想系统地梳理一下. 正好借助<深入Linux内核架构> (Wolfgang Manuere 著,郭旭 译)汇总一下. 首先基础框架篇,Linux ...
- 如何用纯 CSS 为母亲节创作一颗像素画风格的爱心
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/LmrZVX 可交互视频教 ...
- jQuery的on绑定click和直接绑定click区别
状况之外 在之前的公司并没有遇到这个问题,也就没有深究.直到自己换了现在的公司,刚来第二天就开始写别人写到一半的项目,很无奈,不是原生就是jquery,由于项目急,已经来不及切换框架重新布局,只能继续 ...
- Python中的函数(4)
一.传递列表 你经常会发现,向函数传递列表很有用,这种列表包含的可能是名字.数字或者更复杂的对象(如字典). 将列表传递给函数后,函数就能直接访问其内容. 栗子:假设有一个用户列表,我们要和其中每一位 ...
- 杭电 2120 Ice_cream's world I (并查集求环数)
Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_ ...
- BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)
由题目条件显然可以得到状态 f[i][j] 表示以 i 为结尾且 i 后作为断点,共做了 j 次分组的最小代价. 因此转移变得很显然:f[i][j]=min{f[k][j-1]+(s×j+sumT[i ...
- 初次使用xUtils3
由于初学Android,之前也没接触过xUtils2.X系列,所以xUtils3.X弄了很久还是一直报空指针异常,后来看了Sample找到问题所在,现在把xUtils3.X的使用写来供像我这样的小白参 ...
- luogu1463 [HAOI2007]反素数
以下证明来自算法竞赛进阶指南 引理一: 答案就是 \([1,n]\) 之间约数个数最多的最小的数. 证明: 记 \(m\) 是 \([1,n]\) 之间约数个数最多的最小的数.则 \(\forall ...
- NYOJ90-整数划分,经典递归思想~~
整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+-+nk, 其中n1≥n2≥-≥nk≥1,k≥1. 正整数 ...