TinyTest 似乎只关心单元测试;然而,Meteor 包可能有 UI 元素,并且引入一个预先制作的 HTML 文件来运行小部件会很有帮助。例如,我们可能想要转换 <table>
使用 DataTables.net 进入网格,然后测试实例化是否正确。
如何在 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();
});
});
最佳答案
我个人认为 TinyTest 不是适合这项工作的工具!您可能会找到如何包含 Asset 包或编写自己的文件加载器,但您很快就会面临需要在测试中查询 DOM 的问题。
以下是我能想到的一些选项:
选项 1:
您可以使用 xolvio:webdriver 访问完全呈现的页面。 。如果您将此包包含在您的 onTest
block 中,那么您应该可以在 TinyTest 测试中访问 wdio。我说应该是因为我根本不使用 TinyTest,但我设计的 webdriver 包可供任何框架使用。按照包自述文件中的说明进行操作,然后执行如下操作:
browser.
init().
url('https://rawgit.com/FortAwesome/Font-Awesome/master/src/test.html').
getSource(function(err, source) {
// you have a fully rendered source here and can compare to what you like
}).
end();
这是一个重量级选项,但可能适合您。
选项 2: 如果您愿意放弃 TinyTest,另一个选择是使用 Jasmine。它支持client unit测试,以便您可以加载执行视觉效果的单元并通过单元测试将其隔离。
选项 3: 您可以围绕您的包创建一个测试应用程序。所以你会:
/package
/package/src
/package/example
/package/example/main.html
/package/example/tests
/package/example/tests/driver.js
现在示例目录是 Meteor 应用程序。在 main.html 中,您将使用您的包,在测试目录下,您可以将您选择的框架(jasmine/mocha/cucumber)与 webdriver 结合使用。我喜欢这种包开发模式,因为您可以测试包,因为它旨在供应用程序使用。
关于javascript - 将 HTML 文件拉入 TinyTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180892/