Testing CoffeeScript in Rails
Join the DZone community and get the full member experience.Join For Free
As I mentioned in an earlier post on testing CoffeeScript in Node.js, Mocha is
elegant, powerful, and a joy to use. But how do you use Mocha with
Rails? Faced with this obstacle, I created MochaRails.
Using should-style assertions and CoffeeScript, Mocha tests are the best-looking implementation of BDD that I have ever seen in any language. For example, here are some simple tests for a Backbone.js View.
describe 'ItemView', -> before -> @itemView = new Backbone.View tagName: 'li' className: 'item' describe "el", -> it 'should return a value', -> expect(@itemView.el).to.exist it "should return an 'li' element", -> @itemView.el.tagName.should.equal "LI" describe '$el', -> it "should have class 'item'", -> @itemView.$el.hasClass('item').should.be.true
Once you've installed MochaRails and added the test above (simple to do, and explained in the project's Readme), open localhost:3000/mocha to execute your test suite. If you've never seen Mocha's clean and elegant report for browser tests, prepare to be impressed.
Mocha also supports asynchronous code with a simple callback mechanism. I highly recommend you browse Mocha's features and give MochaRails a try.
Published at DZone with permission of Chris Smith, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.