Utilizing Jasmine and taking full advantage of the Rails 3.1 asset pipeline. Jasminerice removes any excuse YOU have for not testing your out of control sprawl of CoffeeScript files. This project rocks and uses the MIT-LICENSE.
See guard-jasmine for details.
This gem has been tested and run with Rails 3.1 and 3.2. Just include it in
your Gemfile
:
group :development, :test do
gem "jasminerice"
end
The engine is automatically mounted into your application in the development and test environments. If you'd like to change that behavior, you can change which groups the gem is included in via the gemfile.
Optionally, you can run the installer
rails g jasminerice:install
This will add the required spec.js.coffee
together with a sample spec and
fixture to help get you started.
It will also add a intializer config/initializers/jasminerice.rb
which
can be used for easy setup of Jasminerice's options
Create a file spec/javascripts/spec.js.coffee
with the following content:
#=require_tree ./
In the case where you need access to all your application javascript assets then create the file spec/javascripts/spec.js.coffee
with the following contents:
#=require_tree ./
#=require_tree ../../app/assets/javascripts
This pulls in all your specs from the javascripts
directory into Jasmine:
spec/javascripts/*_spec.js.coffee
spec/javascripts/*_spec.js
spec/javascripts/*_spec.js.erb
The Rails 3.1 asset pipeline using Sprockets and Tilt ensure conversion.
As well you can use the #require
dependency mechanisms in your specs to
pull dependencies. Here's an example spec/javascripts/foo_spec.js.coffee
:
#= require foo
#= require bar
describe "Foo", ->
it "it is not bar", ->
v = new Foo()
expect(v.bar()).toEqual(false)
describe "Bar", ->
it "it is not foo", ->
v = new Bar()
expect(v.foo()).toEqual(false)
For including stylesheets in your specs, Jasminerice uses spec/javascripts/spec.css
.
Use Sprockets directives to include the right css files:
/*
*= require application
*/
Jasminerice makes files located in the spec/javascripts/fixtures
directory available
as fixture. For example, a file spec/javascripts/fixtures/baz.html.haml
with the
following content:
%h2 Test Fixture
%p Using fixtures
is made available under the URL /jasmine/fixtures/baz
. Since Jasminerice automatically
makes a patched version of jasmine-jquery
available in your specs, you can load the baz
fixture in your spec with:
loadFixtures 'baz'
You can also load JSON fixtures, e.g. spec/javascripts/fixtures/json/bar.json
getJSONFixture('bar')
You can declare Jasminerice::SpecHelper (perhaps put inside lib/) to make helpers available to jasminerice fixtures.
So in your lib directory, create the helper, e.g. lib/jasminerice/spec_helper.rb
module Jasminerice
module SpecHelper
def print_a_test
"foo"
end
end
end
Then you can use it in your fixtures, e.g. spec/javascripts/fixtures/bar.html.haml
%h1 Here is my helper
= print_a_test
Now start your server
rails s
Goto
http://localhost:3000/jasmine
and there are your specs.
You can override your current environment's config.assets.debug
configuration per request
by adding ?debug=false
or ?debug=true
to the jasmine path, eg.
http://localhost:3000/jasmine?debug=false
This will concatenate all your css and javascript into single file which can improve your suite's loading speed significantly.
If you use Require.js in your project and need to load your
modules in your jasmine specs, there is an option to prevent jasminerice from automatically
executing the test runner before the modules are defined. This enables you to start the
execution manually whenever you want in your spec/javascripts/spec.js.coffee
file:
#= require your/specs/and/other/stuff
# at the end of this file add:
jasmine.rice.autoExecute = false
define 'jasmine.waitsfor.requirejs', ->
require ['jasmine.waitsfor.requirejs'], ->
jasmine.getEnv().execute()
The shown example defines a dummy module in require.js that is required immediately on the next line. This is a simple hack to wait until require.js has initialized all modules and start the jasmine runner after that.
Of course you can use jasmine.rice.autoExecute = false
also for all other cases where you need
to control when your specs should be executed!
- Brad Phelan (bradphelan@xtargets.com)