【发布时间】:2015-01-26 15:42:37
【问题描述】:
TinyTest 似乎只关心单元测试;然而,Meteor 包可能有 UI 元素,拉入一个预先制作的 HTML 文件来练习一个小部件会很有帮助。例如,我们可能希望使用 DataTables.net 将<table> 转换为网格,然后测试实例化是否正确。
如何在 TinyTest 中使用外部 HTML 文件?
package.js:
Package.onTest(function (api) {
api.use(packageName, where);
api.use(['tinytest', 'http'], where);
// TODO we should just bring in src/test.html - but how to do that with TinyTest?
api.addFiles('src/test.html', where); // this won't magically display the HTML anywhere
api.addFiles('meteor/test.js', where);
});
test.js:
Tinytest.addAsync('Visual check', function (test, done) {
var iconsDropZone = document.createElement('div');
document.body.appendChild(iconsDropZone);
// TODO ideally we'd get src/test.html straight from this repo, but no idea how to do this from TinyTest
HTTP.get('https://rawgit.com/FortAwesome/Font-Awesome/master/src/test.html', function callback(error, result) {
if (error) {
test.fail('Error getting the icons. Do we have an Internet connection to rawgit.com?');
} else {
iconsDropZone.innerHTML = result.content;
test.ok({message: 'Test passed if the icons look OK.'});
}
done();
});
});
【问题讨论】:
-
嘿@Sam-Hatoum,有什么想法吗?
-
了解 Blaze 测试的结构:github.com/meteor/meteor/blob/devel/packages/blaze/…。这个想法是创建 dom 节点而不将它们插入到主文档中。
-
@imslavko:是的,但是如何从存储库中检索 HTML 文件,而不是通过 HTTP.get?目标是在
test.js文件中不包含大量 HTML。 -
添加为资产,然后读取资产?
-
@imslavko:如果我在
Tinytest.addAsync()中调用console.log(Assets.getText('index.html'));,我会得到Assets is not defined。另外,当文件可以(显然)已经通过api.addFiles('src/test.html', 'client');拉到包中时,我必须直接创建一个private
标签: javascript html meteor tinytest