Paul French Paul French - 1 year ago 70
Javascript Question

How to require Javascript files for testing in my RSpec/Capybara/Selenium tests?

I am trying to require some Javascript files for use in my Rails RSpec tests. They are specifically for testing jQuery drag and drop, but I can't seem to find documentation anywhere as to how to require them for testing. I have copied the .js files to

, but I don't know how to tell RSpec to require them. I tried to require them in my spec_helper.rb:

require 'spec/javascripts/helpers/jquery.simulate.js'
require 'spec/javascripts/helpers/jquery.simulate.ext.js'
require 'spec/javascripts/helpers/jquery.simulate.drag-n-drop.js'

But that just gives me

cannot load such file -- spec/javascripts/helpers/jquery.simulate.js (LoadError)


Put test-only Javascript on the page you're testing the same way as you do production Javascript. Move the test support library to your asset directory. Wherever you include the jQuery drag-and-drop library, include the test support library too, but only if the app is in the test environment:

<% if Rails.env.test? %>
   <%= javascript_include_tag 'jquery.simulate.js' %>
   <%= javascript_include_tag 'jquery.simulate.ext.js' %>
   <%= javascript_include_tag 'jquery.simulate.drag-n-drop.js' %>
<% end %>