针对 Rails 3.1+ 的 Jasmine 与 Mocha JavaScript 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9710297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:33:42  来源:igfitidea点击:

Jasmine vs. Mocha JavaScript testing for Rails 3.1+

javascriptruby-on-railsjasminemocha

提问by LupineDev

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 的经验,并且非常喜欢它。有没有人有 Jasmine 和 Mocha 的经验,特别是 Rails?我想知道是否值得切换到。

回答by Peter Lyons

I have done testing in both Jasmine and Mocha. First, switching is relatively easy. The basic describeand itBDD 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.

我已经在 J​​asmine 和 Mocha 中进行了测试。首先,切换相对容易。基本模式describeitBDD 模式是相同的。您将需要更改执行断言的方式并切换到不同的异步测试接口。总的来说,它们具有可比性。

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.

Mocha 的异步接口更简单,更一致。测试和设置可以是同步的,也可以是异步的,这很好。这一点,再加上 TJ Holowaychuck 是一位史诗般的代码诗人,这些都是尝试 Mocha 的好理由。

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-jquery 插件搭配使用时。Mocha 通常与单独的断言库配对,如果您在进行浏览器内测试,则通常是 chai.js,或者用于仅节点测试的 should.js。我对 chai 的assert.equal()界面很满意,但 Jasmine 风格expect($("#central_errors").html()).toContain("must provide a name");对我来说似乎更优雅。我不喜欢expect(42).to.be.above(41)带有点分隔语句的 chai.js风格的界面,因为它在听觉上效果不佳。

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.

归根结底,这是一个个人偏好问题,我强烈建议您花一天左右的时间编写 Mocha 测试而不是 Jasmine,看看感觉如何。完全值得的投资,即使您决定坚持使用 Jasmine,您也会从第一手知识的角度这样做,并且了解解决 Jasmine 解决的一些问题的其他方法。我试过了,我坚持使用 Mocha 部分是因为押注 TJ 是一个不错的选择,但 Jasmine 也是一个成熟、可靠且被广泛采用的库。

回答by jeHymans0n

Check out the Teabag project on github. It specifically allows you to use the asset pipeline along with Mocha, Jasmine, or QUnit.

查看 github 上的 Teabag 项目。它特别允许您将资产管道与 Mocha、Jasmine 或 QUnit 一起使用。

https://github.com/modeset/teaspoon

https://github.com/modeset/teaspoon

Mocha's a pretty great library, but I typically use Jasmine -- for me it's primarily about knowledge and experience with Jasmine, but Mocha has some really cool features -- like letting you know when you're bleeding things into the global scope.

Mocha 是一个非常棒的库,但我通常使用 Jasmine —— 对我来说,它主要是关于 Jasmine 的知识和经验,但 Mocha 有一些非常酷的功能 —— 比如让你知道什么时候你正在将东西渗入全局范围。

Figured it was worth mentioning the project because it specifically allows you to play around with both (in different suites) so you can decide for yourself.

认为值得一提该项目,因为它特别允许您同时使用两者(在不同的套件中),以便您可以自己决定。