【发布时间】:2014-01-24 21:57:36
【问题描述】:
我正在尝试找到测试对象参数不被函数更改的最佳方法。
想象一下这个函数什么都不做:
function underTest (someObject) {
//It does nothing
}
现在我想测试 arg 没有被函数中化。 我知道几乎没有人会做这样的测试,但我是一个整合者(也是一个菜鸟^^)
it("Do not modify params", function () {
var myObj = { property : 1, other : 3};
underTest(myObject);
//??? So
});
问题是:您将如何实现这一目标?
重复对象?
it("Do not modify params", function () {
var myObj = { property : 1, other : 3};
underTest(myObject);
assert.deepEqual({ property : 1, other : 3}, myObj);
});
在函数中使用之前使用框架克隆它?
it("Do not modify params", function () {
var myObj = { property : 1, other : 3};
var clone = clonerFunction(myObj);
underTest(myObj);
assert.deepEqual(clone, myObj);
});
使用 sinonJS 的解决方案(已用于 spy/stub on function)?
【问题讨论】:
标签: node.js unit-testing mocha.js sinon