我正在尝试测试实习生,看看它是否适合测试框架.我试图在实习生中测试以下代码.
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;
Run Code Online (Sandbox Code Playgroud)
该文件位于'js-test-projects/node/lib/HelloWorld.js'中,Intern位于'js-test-projects/intern'.我正在使用实习生的1.0.0分支.每当我尝试包含文件并运行测试时,在"默认为控制台记者"之后我都没有得到任何输出.这是测试文件.
define([
'intern!tdd',
'intern/chai!assert',
'dojo/node!../lib/HelloWorld'
], function (tdd, assert, HelloWorld) {
console.log(HelloWorld);
});
Run Code Online (Sandbox Code Playgroud)
1.假设以下目录结构(基于问题):
js-test-projects/
node/
lib/
HelloWorld.js - `HelloWorld` Node module
tests/
HelloWorld.js - Tests for `HelloWorld`
intern.js - Intern configuration file
intern/
Run Code Online (Sandbox Code Playgroud)
2.您的Intern配置文件应包含有关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' ]
// ...
Run Code Online (Sandbox Code Playgroud)
3.您的测试文件应HelloWorld
使用Intern的Dojo版本加载,如下所示:
define([
'intern!tdd',
'intern/chai!assert',
'intern/dojo/node!./node/lib/HelloWorld.js'
], function (tdd, assert, HelloWorld) {
console.log(HelloWorld);
});
Run Code Online (Sandbox Code Playgroud)
注意:您不具备使用实习生的版本的Dojo加载HelloWorld
节点模块在这个AMD测试中,它仅仅是这样做的一种便捷方式.如果您有一些其他AMD插件,该节点需要一个节点模块,那就完全没问题了.
4.最后,要在Node.js环境中运行测试,请client.js
通过在intern
目录中发出以下命令来使用Intern的节点运行程序:
node client.js config=node/tests/intern
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2460 次 |
最近记录: |