Using Jasmine with Rails 4.1

I had a few problem getting fixtures to work within a Jasmine environment. I was getting the error “ReferenceError: loadFixtures is not defined”

The fixture was an html pages at spec/javascripts/fixtures/form.html, and this was my initial test code:

describe("Rollback prompt", function(){

  beforeEach(function(){
    loadFixtures("form.html");
  });

  describe('check test environment', function(){

    it('should always pass', function(){
      expect(true).toBe(true);
    });

  });

});

I’d copied this format from a rails 3 project where I’d used the jasmine gem successfully.

To get this to work with Rails 4.1, I had to:

Use jasmine-rails gems

I added this to my Gemfile (replacing gem ‘jasmine’)

group :development, :test do
  # JavaScript test environment
  gem 'jasmine-rails'
  gem 'jasmine-jquery-rails'
end

And added a spec_helper at spec/javascripts/helpers/spec_helper.js:

//= require jquery
//= require jasmine-jquery

Mount fixtures separately

I mounted my fixtures via config/initializers/jasmine.rb:

# Map fixtures directory for Jasmine suite
if defined?(JasmineRails)
  JasmineFixtureServer = Proc.new do |env|
    Rack::Directory.new('spec/javascripts/fixtures').call(env)
  end
end

And then updated my config/routes.rb:

  if defined?(JasmineRails)
    mount JasmineRails::Engine => '/specs'
    mount JasmineFixtureServer => '/spec/javascripts/fixtures'
  end

After that my test passed successfully and I was ready to start building by JavaScript functions.

Solution source

These pages were key to me finding this solution:

This entry was posted in JavaScript, Ruby. Bookmark the permalink.