Mar 20, 2012

BackboneConf Schedule Includes Ember.js and Knockout.js

The lineup for the first BackboneConf has been released, and delivering on an implicit promise made in the call for papers, the schedule includes presentations on alternative JavaScript MV* projects: Ember.js will be covered by Yehuda Katz, and Knockout.js by creator Steven Sanderson. It would be amazing if someone could get these two to engage in dialogue contrasting Ember and Knockout, which seem more similar than they are different in their value proposition of data binding with support for computed properties. If you attend BackboneConf, do not miss that particular hallway conversation.

At a reasonable $349, the two-day, single-track conference is a very welcome addition to the growing JavaScript conference scene. The May 30, 2012 keynote will be delivered by Jeremy Ashkenas, creator of both Backbone and CoffeeScript. Sadly, CoffeeScript is nowhere to be seen on the BackboneConf schedule. But if you're wondering, we at CoffeeScript Love can tell you: Writing your Backbone applications in CoffeeScript is a great way to go.

Mar 8, 2012

Testing CoffeeScript in Rails

As I mentioned in an earlier post on testing CoffeeScript in Node.js, Mocha is a flexible and feature-rich test framework for JavaScript. It is elegant, powerful, and a joy to use. But how do you use Mocha with Rails? Faced with this obstacle, I created MochaRails.

MochaRails is a mountable Rails engine that serves a browser-based Mocha test suite, along with your development JavaScript files, via the Asset Pipeline. It loads Chai for both should- and expect-style assertions, although since Mocha is decoupled from the assertion library, you can use another assertion library if you choose (or even write your own.)

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', ->

    it "should return an 'li' element", ->
      @itemView.el.tagName.should.equal "LI"

  describe '$el', ->

    it "should have class 'item'", ->

I would argue that even if you can't or don't want to write your production code in CoffeeScript, you should write your tests in CoffeeScript. By eliminating so much visual noise, CoffeeScripts syntax helps bring the goal of BDD--specs that anyone can read to understand the intended behavior of your system--so much closer to reality.

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.

Now that's clean! It may seem almost too clean with just three tests, but once you have scores of contexts and hundreds of tests, you'll appreciate the aesthetic. Errors are colored red of course, but Mocha errors do not light up the page like a garish billboard.

Mocha also supports asynchronous code with a simple callback mechanism. I highly recommend you browse Mocha's features and give MochaRails a try.

Update: Since I first looked into using Mocha with Rails, a project very similar to MochaRails has appeared: Konacha. It looks like momentum is building behind Mocha and Chai!