Header_image
Jeremy Walker

Software Developer & Social Entrepreneur

Using Jasmine in a Rails Engine

I burnt an hour today trying to use Jasmine in a Rails engine, and found a lot of other people having the same issue, so thought I'd blog to save someone else the same issue.

The Problem

To test a Rails engine, you use a dummy application within the test/spec directory. Various piping is done to link it to your engine, and to merge things like Rakefiles. When trying to test with Jasmine, I was finding that some "piping" was missing, which meant that specs were being looked for in:

/spec/dummy/spec/javascripts

rather than:

/spec/javascripts

The Solution

The final solution came from this blog post, but as it's slightly out of date, I thought I'd provide a new write up.

This solution uses jasminerice. This ceased to be developed a while ago, but they've recently found a new maintainer, so I'm less concerned that I was before about using the project. You also need to use the Github version of jasminerice at the moment. You can do this by adding it to your Gemfile (probably as well as your gemspec):

# Gemspec
s.add_development_dependency "jasminerice"

# Gemfile
gem 'jasminerice', github: 'bradphelan/jasminerice'

The key thing to fix the path issue is then to add the following:

# spec/dummy/config/initializers/jasminerice.rb

Rails.application.config.assets.paths << ENGINE_NAME::Engine.root.join("spec", "javascripts") << ENGINE_NAME::Engine.root.join("spec", "stylesheets")

# Add engine to view path so that spec/javascripts/fixtures are accessible
ActionController::Base.prepend_view_path ENGINE_NAME::Engine.root

You can then create a css spec helper at spec/javascripts/spec.css

/*
 *= require application
 */

and a javascript spec helper at spec/javascripts/spec.js.coffee:

#= require jquery
#= require jquery_ujs
#= require_tree . 
jasmine.getFixtures().fixturesPath = 'fixtures'

Then add your scripts at spec/javascripts/some_spec.coffee:

#= require my_engine/my_model

describe "Something", ->
  it "exists", ->
    expect(1).toEqual(1)
    expect(MyEngine.MyModel).not.toBeUndefined()

Thanks again to Daniel Wanja for getting me to that solution. He deserves the credit.

Share This Post

Comments

No-one has posted a comment yet. Be the first!

Post a Comment

I'd love to hear your thoughts :)