問題描述
我有 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 測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!