我对TDD很陌生,并使用实习生 v4编写单元测试。我已经看过文档了,但编写测试仍然有困难。如果有人能给我指明正确的方向会很感激的。
我的javascript代码如下:
var app = (function(){
let name = 'Mufasa';
let fullname = '';
return {
print: function(surname) {
fullname = `hello ${name} ${surname}`;
console.log(fullname);
return fullname;
}
}
})();
app.print('Vader');
我通过国家预防机制将实习生包括在我的项目中。
我的单元测试文件test1.js如下所示:
const { suite, test } = intern.getInterface('tdd');
const { assert } = intern.getPlugin('chai');
// how do i write my first test case here to check the output when i pass/do not pass the surname parameter
发布于 2017-11-28 13:33:43
由于您使用的是非模块化JS,所以您的应用程序将被加载到全局命名空间中(也就是说,它将以window.app的形式访问)。测试看起来可能如下:
suite('app', () => {
test('print', () => {
const result = app.print('Smith');
assert.equal(result, 'hello Mufasa Smith');
})
});为了让实习生能够调用您的app.print函数,需要加载应用程序脚本。假设您的代码是针对浏览器的,您可以在测试套件中使用Intern的loadScript函数,也可以将脚本作为插件加载。
suite('app', () => {
before(() => {
return intern.loadScript('path/to/script.js');
});
// tests
});或
// intern.json
{
"plugins": "path/to/script.js"
// rest of config
}https://stackoverflow.com/questions/47523769
复制相似问题