如何模拟装载有dojo / node的Node.js模块 [英] How to mock a Node.js module loaded with dojo/node
问题描述
我有一个应用程序,服务器代码为在Node.js上运行并使用Dojo 。我有一个 config
模块定义如下:
I have an application with the server code running on Node.js and using Dojo. I have a config
module defined like:
define([
'dojo/node!nconf',
'dojo/_base/config'
], function (nconf, dojoConfig) {
nconf.argv().file({
file: dojoConfig.baseDir + '/config.json'
});
console.log('-- file name:', dojoConfig.baseDir + '/config.json');
console.log('-- context:', nconf.get('context'));
// ... logic here ...
return nconf.get(nconf.get('context'));
});
为了能够单元测试这个模块,我写了两个模拟:一个用于 nconf
本机模块,一个用于 dojoConfig
。这是测试:
To be able to unit test this module, I've written two mocks: one for the nconf
native module and one for dojoConfig
. Here is the test:
define([
'require',
'intern!object',
'intern/chai!assert'
], function (require, registerSuite, assert) {
registerSuite({
name: 'config utility',
'load default settings': function () {
require.undef('dojo/node!nconf');
require.undef('dojo/_base/config');
require({ map: {
'*': {
'dojo/node!nconf': 'server/utils/tests/nconfMock',
'dojo/_base/config': 'server/utils/tests/dojoConfigMock'
}
}});
require(['../config', './nconfMock'], this.async(1000).callback(
function (config, nconfMock) {
assert.isNotNull(config);
assert.isNotNull(nconf);
// assert.deepEqual(config, nconfMock.contextSettings.test);
}
));
}
});
});
我可以看到我的模拟 dojoConfig
正确加载,而不是 nconf
模块的模拟。在实习生的网络广播中,Dylan提到,映射不考虑该插件,有一种方法可以强制 dojo / node
模块来加载这个 nconfMock
。你会介意给我更多的细节吗?
I can see that my mock of dojoConfig
is correctly loaded, but not the mock of the nconf
module. During a webcast on Intern, Dylan mentioned that the mapping does not consider the plugin, that there's the way to force dojo/node
module to load this nconfMock
. Would you mind to give me more details?
显然,这是冗长的,所以如果这继续是一个常见的请求,我们可能会做一些事情来简化
Obviously, this is verbose, so if this continues to be a common request, we’ll probably do something to make it simpler in the future.
重要提示:没有映射 dojo / node
到 intern / node_modules / dojo / node
,在实际环境中加载我初始的 config
模块失败。映射在 intern.js
文件中完成。报告的错误是:
Important note: Without mapping dojo/node
to intern/node_modules/dojo/node
, the loading of my initial config
module as defined above fails in the Intern environment. The mapping is done in the intern.js
file. The reported error is:
Error: node plugin failed to load because environment is not Node.js
at d:/git/fco2/src/libs/dojo/node.js:3:9
at execModule (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:512:54)
at d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:579:7
at guardCheckComplete (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:563:4)
at checkComplete (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:571:27)
at onLoadCallback (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:653:7)
at d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:758:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
解决方案:如下面的Colin Snover建议,我现在使用Mockery。我也不使用上下文 require
,只有默认的。这是一个使用Dojo工具包版本1.9.3的(简化)解决方案。
Solution: As suggested by Colin Snover below, I now use Mockery. I also do NOT use the contextual require
, only the default one. Here is a (simplified) solution working with the version 1.9.3 of the Dojo toolkit.
define([
'intern!object',
'intern/chai!assert',
'intern/node_modules/dojo/node!mockery',
'./nconfMock'
], function (registerSuite, assert, mockery, nconfMock) {
registerSuite({
name: 'config utility',
teardown: function () {
mockery.disable();
mockery.deregisterAll();
require({ map: { '*': { 'dojo/_base/config': 'dojo/_base/config' } } });
require.undef('dojo/_base/config');
require.undef('server/utils/config');
},
'load default settings': function () {
mockery.enable();
mockery.registerMock('nconf', nconfMock);
require({ map: { '*': { 'dojo/_base/config': 'server/utils/tests/dojoConfigMock' } } });
require.undef('dojo/_base/config');
require.undef('server/utils/config');
require(
['server/utils/config'],
this.async(1000).callback(function (config) {
assert.isNotNull(config);
assert.deepEqual(config, nconfMock.contextSettings.test);
})
);
}
});
});
谢谢,Dom
推荐答案
为了模拟Node.js依赖关系,您可能希望简单地使用各种可用项目之一来嘲笑Node.js模块。 Mockery 是一个不错的选择,因为它是独立的。
In order to mock a Node.js dependency, you will probably want to simply use one of the various available projects for mocking Node.js modules. Mockery is a good choice since it’s stand-alone.
由于看起来您使用的是 dojo / node
而不是Intern中的一个,您可以这样做:
Since it looks like you’re using dojo/node
and not the one from Intern, in your case, you’d do it like this:
define([
'intern!object', 'dojo/node!mockery', 'dojo/Deferred', 'require'
], function (registerSuite, mockery, Deferred, require) {
var moduleUsingMock;
registerSuite({
setup: function () {
var dfd = new Deferred();
mockery.enable();
mockery.registerMock('module-to-mock', mockObject);
require([ 'module-using-mock' ], function (value) {
moduleUsingMock = value;
dfd.resolve();
});
return dfd.promise;
},
teardown: function () {
mockery.disable();
},
'some test': function () {
moduleUsingMock.whatever();
// ...
}
});
});
这篇关于如何模拟装载有dojo / node的Node.js模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!