问题描述
我有 Jasmine 的经验,并且非常喜欢它.有没有人有 Jasmine 和 Mocha 的经验,特别是 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 都做过测试.首先,切换相对容易.基本的 describe
和 it
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 的好理由.
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("must provide a name");
对我来说似乎更优雅.我不喜欢带有点分隔句子的 chai.js expect(42).to.be.above(41)
样式界面,因为它在听觉上效果不佳.
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.
归根结底,这是一个个人偏好问题,我强烈建议您花一天左右的时间编写 Mocha 测试而不是 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 与 Mocha JavaScript 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!