This project is intended to make it a little easier to integrate Jasmine into your workflow, particularly if you're working in Rails 3.1 or later. (If you're on earlier versions of Rails, I'd suggest directly using the combination of Pivotal's jasmine gem and jasmine-headless-webkit.)
By bundling this gem and configuring your project, you can expect to:
- Be able to run Jasmine specs from the command line (and fast) with John Bintz's excellent jasmine-headless-webkit
- Be able to run Jasmine specs in a browser (wherever you choose to mount the jasmine-rails engine)
- Write specs or source in CoffeeScript, leveraging the asset pipeline to pre-process it
Install qt for its headless webkit widget. The easiest way (on a Mac) that I've found is to use homebrew:
brew install qt
For help installing the qt libs on other platforms, the I'd recommend perusing capybara-webkit driver's documentation, becuse it has the same dependency.
Add jasmine-rails to your Gemfile, like so
group :test, :development do gem 'jasmine-rails' end
Now, we'll use the jasmine gem to initialize a jasmine.yml configuration file:
bundle exec jasmine init
And remove some files the jasmine gem creates that you won't need:
Next, edit the generated
If you were to run:
bundle exec jasmine-headless-webkit --color
You'd hopefully see something like:
Running Jasmine specs... PASS: 0 tests, 0 failures, 0.001 secs.
I encourage you to explore John Bintz's excellent jasmine-headless-webkit's documentation for more ideas, like creating a Rake task or running it on a display-less CI server.
Just mount jasmine-rails by adding something like this to your routes.rb:
mount JasmineRails::Engine => "/specs" unless Rails.env.production?
Now when you run
bundle exec rails s, and navigate to http://localhost:3000/specs, you should see a Jasmine spec runner in your browser.
When debugging, I append the query param "debug_assets=true" like so: http://localhost:3000/specs?debug_assets=true.
This is telling the asset pipeline to include each of your scripts in individual
<script> tags. Seeing each script loaded separately makes debugging much easier for me.
Even though they both read from the same config file, it's certainly possible that your specs will pass in the browser and fail from the command line. In this case, you can try to debug or analyze what's going on by using the "--keep" flag from jasmine-headless-webkit.
bundle exec jasmine-headless-webkit --keep
If the tests fail, jasmine-headless-webkit will leave its generated spec runner HTML file persisted in your rails root folder. It'll be named something like "specrunner.48160.html".
Guard is a great tool for triggering spec runs when files change. To use it, you can bundle these gems:
group :test, :development do ... gem 'guard-rails-assets' gem 'guard-jasmine-headless-webkit' ... end
In my Guardfile, this configuration is working well for me:
Finally, to run guard, just:
bundle exec guard