什么是Sinon.js(JS模拟库)的替代品?

时间:2012-07-13 14:09:19

标签: unit-testing sinon

Sinon.js有什么有价值的替代品吗?

感谢。

3 个答案:

答案 0 :(得分:1)

不太高级,但您可以查看Jack

答案 1 :(得分:1)

Testdouble.js

还有一个名为testdouble.js的库。这是一种比sinon.js更面向对象的方式。

另外,来自testdouble的this article解释了sinon.js和testdouble.js之间的区别。

实施例

var td = require('testdouble');

var fetch = td.function();
td.when(fetch(42)).thenReturn('Jane User');

fetch(42); // -> 'Jane User'

答案 2 :(得分:0)

我刚开始一个名为candy-wrapper的新项目,在某些情况下可能是Sinon的替代品: https://www.npmjs.com/package/candy-wrapper

以下是如何使用它的一些示例,如果有人对如何使其更好有任何见解,我会很乐意反馈:

var Wrapper = require("candy-wrapper");

// a simple test object
var myDrone = {
    name: "DJI",
    fly: function(direction) {
        return true;
    }
}

new Wrapper(myDrone, "name");
new Wrapper(myDrone, "fly");

myDrone.fly("north");
myDrone.fly("west");

// evaluating previous calls through the 'historyList' and 'Filters'
myDrone.fly.historyList.filterFirst().expectCallArgs("north"); // true
myDrone.fly.historyList.filterSecond().expectCallArgs("east"); // false
myDrone.fly.expectReportAllFailtures(); // throws an error about the "east" expecation failing

// modifying behavior using 'Triggers'
myDrone.fly.triggerOnCallArgs("east").actionReturn(false); // will return 'false' when called with "east"
myDrone.fly("east"); // false
myDrone.fly("west"); // true (the default return value)

// working with properties
myDrone.name.triggerOnSet().actionThrowException(new Error("do not set the name"));
myDrone.name = "Bob"; // throws Error: "do not set the name"
var ret = myDrone.name; // ret = "DJI"