【发布时间】:2015-07-06 21:11:01
【问题描述】:
我尝试测试的代码依赖于 RequireJs loader plugins。以requirejs/text 为例:
require(['text!templates/foo'], function (data) {
// handle loaded data
});
对于特定的单元测试,我正在尝试模拟text!templates/foo 的响应并用与测试相关的响应覆盖:
it('should load a template', function (done) {
// TODO: mock 'text!templates/foo' here to return 'mock_data'
// templateViewer uses the text plugin internally to do the actual loading
templateViewer.templateFor('foo', function (error, templateData) {
expect(templateData).toEqual('mock_data');
done();
});
});
我查看了 RequireJs 依赖项模拟 solutions,尤其是 Squire.js,但似乎它们都适合模拟常规依赖项而不是插件响应。
我还查看了像 sinon 这样的存根库,可能会替换实际的 require 调用,但这似乎有问题。
推荐的做法是什么?我不想在我的 requirejs 配置中用模拟插件替换整个 text 插件,只是在特定测试中覆盖它的一些响应。
我的设置是node+mocha+requirejs
编辑
请查看此示例小提琴项目以了解我与 Squire 的问题:
http://runnable.com/VUBoI0ex6v9Gs-BJ/squirejs-with-plugins-for-node-js-and-hello-world
【问题讨论】:
标签: javascript unit-testing mocking requirejs squirejs