我不知道如何在 Meteor 中进行测试驱动开发。
我在文档或常见问题解答中没有看到相关提及,也没有看到任何示例之类的东西。
我看到一些包在使用 Tinytest。
我需要开发人员的回复,关于这方面的路线图是什么。大致如下:
- 可能可以,但没有文档,需要自己摸索
- Meteor 没有构建可测试的应用程序的方式
- 计划推出该功能
- 等等
我不知道如何在 Meteor 中进行测试驱动开发。
我在文档或常见问题解答中没有看到相关提及,也没有看到任何示例之类的东西。
我看到一些包在使用 Tinytest。
我需要开发人员的回复,关于这方面的路线图是什么。大致如下:
更新3: 自Meteor 1.3起,meteor包含一个测试指南,提供逐步说明以进行单元、集成、验收和负载测试。
更新2: 截至2015年11月9日,Velocity不再得到维护。Xolv.io将精力集中在Chimp上,并且Meteor Development Group必须选择官方测试框架。
更新: Velocity是自0.8.1版本起Meteor的官方测试解决方案。
目前关于使用Meteor进行自动化测试的文章并不多。我希望Meteor社区能够在正式文档中确立测试最佳实践之前不断发展。毕竟,Meteor本周已经达到了0.5版本,事情仍在迅速变化。
好消息是:你可以使用Node.js测试工具来测试Meteor。
对于我的Meteor项目,我使用Mocha运行单元测试,并使用Chai进行断言。如果你不需要Chai的全部功能,我建议使用should.js代替。目前我只有单元测试,但你也可以使用Mocha编写集成测试。
务必将你的测试放在“tests”文件夹中,这样Meteor就不会尝试执行你的测试。
Mocha支持CoffeeScript,这是我在Meteor项目中选择的脚本语言。这里有一个示例Cakefile,其中包含运行Mocha测试的任务。如果您正在使用JS与Meteor,可以根据需要适应Makefile命令。# 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项目编写和运行单元测试了!
Meteor.whatever
函数时遇到了问题。我得到了“Meteor未定义”的错误。有没有一种明确要求Meteor的方法来解决这个问题? - Christian SchlenskerMeteor
对象是无法访问的,Meteor公开的任何模型依赖项也是如此。改进这个过程将涉及在Mocha中实例化应用程序的环境,并将Meteor
对象暴露给您的测试。一旦我有一个更完整的测试解决方案,我会确保更新这个答案。同时,如果您有任何问题或建议改进,请随时与我联系。 - BlackcoatModel = new Meteor.Collection "Model"
时的 Meteor
对象。 mocha tests --recursive --compilers coffee:coffee-script --ui bdd --colors --reporter spec ~/meteor-app/collections/tags.coffee:2 this.Tags = new Meteor.Collection('tags'); ^ ReferenceError: Meteor is not defined
- Loren大家好,看这里 laika - Meteor全新的测试框架 http://arunoda.github.io/laika/
您可以同时测试服务器和客户端。
免责声明:我是Laika的作者。
我一直在与Naomi Seyfer (@sixolet)合作,加入meteor核心团队,帮助将所有这些问题的明确答案放入文档中。
我已经向meteor核心提交了一个初步的拉取请求,解决了1和2:https://github.com/meteor/meteor/pull/573。
最近我也回答了这个问题: 如何运行meteor测试?
我认为@Blackcoat已经明确地回答了上面的第3个问题。
关于奖金4,我建议至少使用circleci.com对您自己的应用程序进行持续集成。他们目前支持@Blackcoat所描述的用例。我有一个项目,在其中我已成功地使用coffeescript编写了测试,并使用mocha运行了单元测试,基本上就像@Blackcoat所描述的那样。这个视频介绍了基础知识:https://www.eventedmind.com/feed/meteor-testing-packages-with-tinytest
一旦你理解了这个概念,你会想要查看 tinytest
的公共API文档。现在,唯一的文档在 tinytest
包的源代码末尾: https://github.com/meteor/meteor/tree/devel/packages/tinytest
此外,视频讲解了 test-helpers
,你可能想要查看这里所有可用的帮助程序:
https://github.com/meteor/meteor/tree/devel/packages/test-helpers
每个文件中通常都有一些文档。
深入挖掘meteor包中的现有测试将提供很多示例。一种做法是在meteor源代码的包目录中搜索 Tinytest.
或 test.
https://github.com/mad-eye/meteor-mocha-web
我发现我还需要使用router Atmosphere package,以便创建'/tests'路由来运行并显示测试结果,因为我不想让它在每次加载我的应用程序时都变得杂乱无章。
meteor-mocha-web
和 mocha-phantomjs
来自动化测试和进行 CI。这就是我们使用的方式。完全透明--我是 meteor-mocha-web
的维护者之一。 - jagill
it('sends all todos for a public list when logged in', (done) => {
const collector = new PublicationCollector({userId});
collector.collect('Todos.inList', publicList._id, (collections) => {
chai.assert.equal(collections.Todos.length, 3);
done();
});
});
Meteor.startup ->
Meteor.call 'shouldTest', (err, shouldTest) ->
if err? then throw err
if shouldTest then runTests()
# Dynamically load and run mocha. I factored this out in a separate method so
# that I can (re-)run the tests from the console whenever I like.
# NB: This assumes that you have your mocha/chai scripts in .../public/mocha.
# You can point to a CDN, too.
runTests = ->
$('head').append('<link href="/mocha/mocha.css" rel="stylesheet" />')
$.getScript '/mocha/mocha.js', ->
$.getScript '/mocha/chai.js', ->
$('body').append('<div id="mocha"> </div>')
chai.should() # ... or assert or explain ...
mocha.setup 'bdd'
loadSpecs() # This function contains your actual describe(), etc. calls.
mocha.run()
...并且在服务器端:
Meteor.methods 'shouldTest': -> true unless Meteor.settings.noTests # ... or whatever.
正如Blackcout所说,Velocity 是Meteor的官方TDD框架。但是目前velocity的网页没有提供良好的文档。因此我建议您观看:
另一个选项是自0.6.0版本以来很容易地通过本地智能包运行整个应用程序,仅需最少量的代码在外部包中引导应用程序(可能调用应用程序基础的某个智能包)。
然后,您可以利用Meteor的Tinytest来测试Meteor应用程序,这非常适合此类测试。