我正在学习将sinon与nodeunit一起使用,专门用于进行模拟。 recommended approach将使用sinon-nodeunit。根据the documentation,模拟应该通过mock参数的test方法提供给每个测试函数。

但是,这似乎不起作用。如果我在包含以下内容的文件上运行nodeunit

require('sinon-nodeunit');

exports['test sinon-nodeunit'] = function (test) {
    mock = test.mock({});
    test.done();
};


—我明白了:

$ nodeunit test/test-sinon-nodeunit.js

test-sinon-nodeunit.js
✖ test sinon-nodeunit

TypeError: Object #<Object> has no method 'mock'
    at /home/malkovich/test/test-sinon-nodeunit.js:4:17
    at Object.runTest (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:54:9)
    at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:90:21
    at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:508:13
    at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:118:13
    at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:134:9
    at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:507:9
    at Object.concatSeries (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:147:23)
    at Object.runSuite (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:79:11)
    at Object.runModule (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:114:13)


FAILURES: 1/1 assertions failed (84ms)


我已经通过sinon安装了sinon-nodeunit(1.1.1)和npm install(0.1.2)的当前版本。

最佳答案

在sinon-nodeunit使用的节点0.5.x及更高版本中删除了require.path。克里斯蒂安·约翰森(Christian Johansen)刚刚推动了sinon-nodeunit的更改,该更改应与应该修复此问题的节点0.6.2(和0.5.x系列)兼容...它已在sinon-nodeunits github页面上可用,但仍未达到npm。 ..

更新:应该提到sinon-nodeunit已停产,因为创建者完全专注于BusterJS,而IMHO是更好的框架。

10-08 11:07