更新 3:从 Meteor 1.3 开始,meteor 包含一个 testing guide,其中包含有关单元、集成、验收和负载测试的分步说明。
更新 2:截至 2015 年 11 月 9 日,Velocity is no longer maintained。 Xolv.io 将精力集中在Chimp 和Meteor Development Group must choose an official testing framework。
更新:从 0.8.1 开始,Velocity 是 Meteor's official testing solution。
目前关于使用 Meteor 进行自动化测试的文章不多。我希望 Meteor 社区在正式文档中建立任何内容之前发展测试最佳实践。毕竟本周 Meteor 达到了 0.5,而且事情还在迅速变化。
好消息:您可以将 Node.js testing tools 与 Meteor 一起使用。
对于我的 Meteor 项目,我使用 Mocha 运行单元测试,并使用 Chai 进行断言。如果您不需要 Chai 的完整功能集,我建议您改用 should.js。我目前只有单元测试,不过你也可以用 Mocha 编写集成测试。
请务必place your tests in the "tests" folder,以免 Meteor 尝试执行您的测试。
Mocha 支持CoffeeScript,这是我为 Meteor 项目选择的脚本语言。这是一个sample Cakefile,其中包含运行 Mocha 测试的任务。如果您在 Meteor 中使用 JS,请随意调整 Makefile 的命令。
您的 Meteor 模型需要稍作修改才能将自己暴露给 Mocha,这需要了解 Node.js 的工作原理。将每个 Node.js 文件视为在其自己的范围内执行。 Meteor 自动将不同文件中的对象相互公开,但普通的 Node 应用程序(如 Mocha)不会这样做。为了使我们的模型可以被 Mocha 测试,export 每个 Meteor 模型都使用以下 CoffeeScript 模式:
# Export our class to Node.js when running
# other modules, e.g. our Mocha tests
#
# Place this at the bottom of our Model.coffee
# file after our Model class has been defined.
exports.Model = Model unless Meteor?
...在 Mocha 测试的顶部,导入您要测试的模型:
# Need to use Coffeescript's destructuring to reference
# the object bound in the returned scope
# http://coffeescript.org/#destructuring
{Model} = require '../path/to/model'
这样,您就可以开始使用 Meteor 项目编写和运行单元测试了!