问题描述
我有Jasmine的经验,并且非常喜欢它。有没有人有茉莉花和摩卡的经验,特别是对于Rails?我想知道是否值得转换。
I have experience with Jasmine and do like it quite a bit. Does anyone have experience with both Jasmine and Mocha, specifically for Rails? I am wondering if it's worth switching to.
推荐答案
我已经在Jasmine和Mocha中进行过测试。首先,切换相对容易。基本的描述
和它
BDD模式是相同的。您将需要更改断言的方式并切换到异步测试的不同接口。总的来说它们具有可比性。
I have done testing in both Jasmine and Mocha. First, switching is relatively easy. The basic describe
and it
BDD pattern is identical. You will need to change how you do your assertions and switch to a different interface for asynchronous tests. Overall they are comparable.
Mocha的异步接口更简单,更一致。测试和设置可以是同步的也可以是异步的,这很好。这个以及TJ Holowaychuck是史诗代码诗人的事实是尝试摩卡的好理由。
Mocha's asynchronous interface is much simpler and more consistent. Tests and setup can be either synchronous or asynchronous, which is great. This, plus the fact that TJ Holowaychuck is an epic code poet are good reasons to try Mocha.
我认为Jasmine匹配器更容易阅读和更优雅,尤其是当与jasmine-jquery插件配对时。 Mocha通常与用于断言的单独库配对,如果您正在进行浏览器内测试,则通常是chai.js,或者是仅用于节点测试的should.js。我对chai的 assert.equal()
接口很满意,但Jasmine样式 expect($(#central_errors)。html())。 toContain(必须提供一个名字);
对我来说似乎更优雅。我不喜欢chai.js expect(42).to.be.above(41)
样式界面,带有点分隔的句子,因为它不能正常工作aurally。
I do think the Jasmine matchers are easier to read and more elegant, especially when paired with the jasmine-jquery plugin. Mocha is usually paired with a separate library for assertions, often chai.js if you are doing in-browser testing or should.js for node-only testing. I am happy with chai's assert.equal()
interface, but the Jasmine style expect($("#central_errors").html()).toContain("must provide a name");
seems more elegant to me. I am not a fan of the chai.js expect(42).to.be.above(41)
style interface with dot-delimited sentences because it doesn't work well aurally.
最终,这是一个个人偏好问题,我强烈建议您花一天左右的时间来编写摩卡测试代替Jasmine,看看它的感受。完全是一项有价值的投资,即使你决定坚持使用Jasmine,你也会从第一手知识的地方这样做,并意识到其他方法可以解决Jasmine解决的一些问题。我尝试了它并且我坚持使用Mocha,部分原因是投注TJ是一个不错的选择,但Jasmine也是一个成熟,稳固且广泛采用的库。
Ultimately, this is a personal preference question and I highly encourage you to just spend a day or so writing Mocha tests instead of Jasmine and see how it feels. Totally a worthwhile investment, even if you decide to stick with Jasmine you will be doing so from a place of first-hand knowledge and have an awareness of other ways to solve some of the problems Jasmine solves. I tried it and I'm sticking with Mocha partly because betting on TJ is a good bet, but Jasmine is also a mature, solid, and widely adopted library.
这篇关于对于Rails 3.1+的Jasmine vs. Mocha JavaScript测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!