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

[Unit Testing] Unit Test a Function that Invokes a Callback with a Sinon Spy的更多相关文章

  1. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  2. Unit Testing, Integration Testing and Functional Testing

    转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...

  3. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

  4. [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 ...

  5. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  6. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  7. Unit Testing PowerShell Code with Pester

    Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...

  8. C# Note36: .NET unit testing framework

    It’s usually good practice to have automated unit tests while developing your code. Doing so helps y ...

  9. [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 ...

随机推荐

  1. Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级

    Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级 // Find components upward function findComponen ...

  2. v8引擎详解

    引用网址: https://blog.csdn.net/swimming_in_it_/article/details/78869549 前言 JavaScript绝对是最火的编程语言之一,一直具有很 ...

  3. 如何改android device monitor文件的权限

    adb.exe在c/Android/platform-tools目录下,在这个目录下打开终端,然后adb shell,然后su http://blog.csdn.net/u012719153/arti ...

  4. 自定义tableView通用MVC设计

  5. mongdb查询数据并且返回数据条数

    var totall; var a = db.db("Magiccat").collection("jishi_content").find().count({ ...

  6. luogu P1238 走迷宫--DFS模板好(水)题

    题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...

  7. centos7 rsync+inotify软件实现集群服务的数据备份(二)

    上一篇文章记录了怎么安装rsync以及怎么使用该服务备份数据,但是在集群中需要实时备份客户发过来的相关数据,这样在使用命令或者定时任务的方式执行备份, 就满足不了整个服务的需求了. inotify是一 ...

  8. react深入 - 手写实现react-redux api

    简介:简单实现react-redux基础api react-redux api回顾 <Provider store>把store放在context里,所有子组件可以直接拿到store数据 ...

  9. Python面向对象之模块和包

    模块 模块的概念 模块是Python程序架构的一个核心概念 所有以.py结尾的源文件都是一个模块: 模块名也是标识符,需要遵循标识符的命名规则: 在模块中定义的全局变量,类,函数,都是直接给外界使用的 ...

  10. python对象销毁(垃圾回收)

    Python 使用了引用计数这一简单技术来跟踪和回收垃圾. 在 Python 内部记录着所有使用中的对象各有多少引用. 一个内部跟踪变量,称为一个引用计数器. 当对象被创建时, 就创建了一个引用计数, ...