Thoughts on development, design and the world we live in.

Getting Started with Jasmine and Rails 3.2

By sarah in Uncategorized. Posted on March 3rd

tldr; check out the git repo

Some good folks have been putting together gems that really help getting started with Javascript testing in Rails, which became somewhat more challenging with the Rails 3.1 asset pipeline.  I decided to dig through some of this and get up to speed with a fresh install of Rails 3.2.2 using my favorite JS testing framework, the BDD-style Jasmine.

I started with Derek Hammer’s testing coffeescript tutorial — I’m not sold on coffeescript, so I reimplemented the first “sanity” spec in plain old javascript.

Note: do not name your .coffee files the same root name as your .js file — only one of them will be executed twice.  Here’s a comparison of the syntax:

Coffeescript Jasmine Spec:

Javascript Jasmine Spec:

Ok, I’m almost won over to coffeescript by this example — it’s nice that I can write my specs in coffeescript and code in Javascript (and I assume vice versa), so I can experiment without having to make a binary decision.

I can see failing specs by running rails s and going to http://localhost:3000/jasmine and it looks like this (when I’ve defined the helloWorld() function but it returns the wrong thing):

Even better, using jasmine-guard, I can see this on the command line:

Summary of getting this all working:

Using rails 3.2.2…

rails new jasminerice-example
$cd jasminerice-example

add the following to your Gemfile

I got a “SHA1 mismatch” error, fixed with


in spec/javascripts/spec.js

in spec/javascripts/spec.css

Now any coffee or js you put in spec/javascripts will be executed when you rails s and go to http://localhost:3000/jasmine or see your specs run on the command line with:

Happy Testing!

By sarah | Posted in Uncategorized | Comments (4)


  1. Posted March 4, 2012 at 9:20 am | Permalink

    I noticed that my keydown tests are failing if i run them with guard - I assume guard is using phantomjs?

  2. Posted March 8, 2012 at 3:05 pm | Permalink

    Yes! Note: I installed it with brew.

  3. Russ Egan
    Posted November 6, 2012 at 11:43 pm | Permalink

    Thanks, this was a huge help.

  4. Ed Allen
    Posted December 11, 2012 at 9:07 am | Permalink

    Tried laying out a project from your instructions above yesterday. Can write specs in CoffeeScript successfully and can test JavaScript functions using it, but a function in CoffeeScript file /app/assets/javascripts/hello.js.coffee is not found for testing, even if it loads and runs in development mode and is included in rake assets:precompile.

    17:23:06 - INFO - sanity
    17:23:06 - INFO - ✘ can convert a coffeescript source file to js and use it
    17:23:06 - INFO - ➤ ReferenceError: Can’t find variable: helloCoffee in trial_spec.js on line 17
    17:23:06 - ERROR - 5 specs, 1 failure

    Will try your repo and see what’s different.

Post a Comment

Your email is never shared. Required fields are marked *