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);
});
});

Docs

最新文章

  1. Tomcat源码导入eclipse的步骤
  2. 我是如何反编译D-Link路由器固件程序并发现它的后门的
  3. SQLServer的学习场景(关于row_number()和COALESCE()的使用)
  4. Win8.1RTM英文版安装中文语言包的两种方法
  5. (转)高性能网站架构之缓存篇—Redis集群搭建
  6. FreeMarker的教程
  7. Hibernate之jpa实体映射的三种继承关系
  8. windows上自动设置java环境变量的脚本
  9. Sql Server来龙去脉系列之四 数据库和文件
  10. SQL效率低下原因主要有
  11. 工作中遇到的浏览器差别(就不叫IE6bug了)
  12. twemproxy接收流程探索——twemproxy代码分析正编
  13. 基于Spark的用户行为路径分析
  14. flask中jinjia2模板引擎详解4
  15. 关于String的一些基础小题目
  16. put your hands up PHPLFI +CVE
  17. python使用redis
  18. 洛谷P4281 紧急集合 / 聚会
  19. hdu3038How Many Answers Are Wrong(带权并查集)
  20. WPF命令(Command)介绍、命令和数据绑定集成应用

热门文章

  1. Android典型界面设计-访网易新闻实现双导航tab切换
  2. 【简●解】[ZJOI2005]午餐
  3. POJ-3624-背包问题
  4. IO之Object流举例
  5. Centos7中 mysql5.7 用户 创建 、授权、远程登录
  6. tornado框架基础11-tornado异步
  7. linux下solr5.0.0环境搭建
  8. SVN服务器的部署与安装
  9. python022 Python3 面向对象
  10. Android TransitionDrawable:过渡动画Drawable