3

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 spec/javascripts/helpers, 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)

2
  • rubylearning.com/satishtalim/including_other_files_in_ruby.html Commented Apr 14, 2016 at 8:30
  • Thanks, but requiring *.rb files is not my issue, its the *.js files to be loaded in the browser in the test environment only for the purposes of simulating jquery drag and drop Commented Apr 14, 2016 at 8:51

1 Answer 1

3

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 %>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You, this was exactly what I was after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.