将 sinon 模拟与 nodeunit 一起使用
我正在学习将 sinon 与 nodeunit 一起使用,专门用于进行模拟。 推荐的方法是使用sinon-nodeunit.根据文档,模拟应该可以通过每个测试函数的 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
(1.1.1) 和 sinon- 的当前版本nodeunit
(0.1.2) 通过 npm install
。
I'm learning to use sinon with nodeunit, specifically to do mocking. The recommended approach is to use sinon-nodeunit. According to the documentation, mocks should be available via the mock
method of the test
parameter to each test function.
However, this doesn't seem to work. If I run nodeunit
on a file containing the following —
require('sinon-nodeunit');
exports['test sinon-nodeunit'] = function (test) {
mock = test.mock({});
test.done();
};
— I get this:
$ 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)
I've installed current versions of both sinon
(1.1.1) and sinon-nodeunit
(0.1.2) via npm install
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
require.path 在节点 0.5.x 及更高版本中被删除,这是 sinon-nodeunit 使用的。 Christian Johansen 刚刚推动了 sinon-nodeunit 的一项更改,该更改应该与 Node 0.6.2(和 0.5.x-series)兼容,这应该可以解决这个问题...它已经在 sinon-nodeunits github 页面上可用,但仍然没有到达 npm。 ..
更新:应该提到的是,sinon-nodeunit 已经停产,因为创建者完全专注于 BusterJS,恕我直言,这是一个更好的框架。
require.path was removed in node 0.5.x and higher, which sinon-nodeunit uses. Christian Johansen just pushed a change in sinon-nodeunit which should be compatible with node 0.6.2 (and 0.5.x-series) which should fix this... It's already available on sinon-nodeunits github page, still hasnt hit npm though...
update: should be mentioned that sinon-nodeunit has been discontinued, as the creator is full focused on BusterJS which IMHO is a much better framework.