我正在尝试测试实习生,看看它是否适合测试框架。我正在尝试在 Intern 中测试以下代码。
var HelloWorld;
HelloWorld = (function () {
function HelloWorld (name) {
this.name = name || "N/A";
}
HelloWorld.prototype.printHello = function() {
console.log('Hello, ' + this.name);
};
HelloWorld.prototype.changeName = function(name) {
if (name === null || name === undefined) {
throw new Error('Name is required');
}
this.name = name;
};
return HelloWorld;
})();
exports = module.exports = HelloWorld;
该文件位于“js-test-projects/node/lib/HelloWorld.js”,实习生位于“js-test-projects/intern”。我正在使用 Intern 的 1.0.0 分支。每当我尝试包含文件并运行测试时,在“默认为控制台报告器”之后我都没有得到任何输出。这是测试文件。
define([
'intern!tdd',
'intern/chai!assert',
'dojo/node!../lib/HelloWorld'
], function (tdd, assert, HelloWorld) {
console.log(HelloWorld);
});
最佳答案
1.假设以下目录结构(根据问题):
js-test-projects/
node/
lib/
HelloWorld.js - `HelloWorld` Node module
tests/
HelloWorld.js - Tests for `HelloWorld`
intern.js - Intern configuration file
intern/
2. 你的实习生配置文件应该包含
node
的信息。包和任何要运行的套件:// ...
// Configuration options for the module loader
loader: {
// Packages that should be registered with the loader in each testing environment
packages: [ 'node' ]
},
// Non-functional test suite(s) to run
suites: [ 'node/tests/HelloWorld' ]
// ...
3. 你的测试文件应该加载
HelloWorld
使用 Intern 版本的 Dojo,如下所示:define([
'intern!tdd',
'intern/chai!assert',
'intern/dojo/node!./node/lib/HelloWorld.js'
], function (tdd, assert, HelloWorld) {
console.log(HelloWorld);
});
注:您不必使用实习生版本的 Dojo 来加载
HelloWorld
这个 AMD 测试中的 node 模块,它只是一种方便的方法。如果您有一些其他 AMD 插件需要节点模块,那很好。4. 最后,要在 Node.js 环境中运行测试,请使用 Intern 的
client.js
通过从 intern
中发出以下命令来运行节点运行器目录:node client.js config=node/tests/intern
关于intern - 无法让实习生运行 Node.js 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466115/