4

In my ruby app I have a page with some javascript function that is responsible for filling a form field. The problem is that when I try to make a test with rspec for this page the javascript doesn't seem to run, resulting in an error on the form.

Any way to solve this problem?

EDIT (Using Fred suggestion)

My spec_helper.rb file looks like this:

require 'capybara/rspec'
require 'factory_girl_rails'
require 'capybara/poltergeist'

require 'support/mailer_macros'
require 'support/test_helper'

Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.include FactoryGirl::Syntax::Methods

  config.include Capybara::DSL

  config.include(MailerMacros)
  config.include(TestHelper)
  config.before(:each) { reset_email }

  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

end

And my test looks like this:

require 'rails_helper'

RSpec.describe "Courses", type: :request do
  let(:user) { FactoryGirl.create(:user) }

  it "should create, show, edit and delete a course" do
    login user
    visit course_types_path("en")
    fill_in "course_type_name", :with => "driving"
    click_button "Add"
    visit courses_path("en")
    current_path.should eq(courses_path("en"))
    click_link "Create new course"
    current_path.should eq(new_course_path("en"))
    select "driving", from: "course_course_type_id"
    fill_in "course_name", :with => "course 1"
    fill_in "course_price", :with => "12"
    click_link "add_date_link"
    select "John Doe", from: "course_teacher_id"
    fill_in "course_max_students", :with => "1"
    fill_in "course_address", :with => "Rua Adriano Correia de Oliveira A, Laranjeiro, Portugal"
    click_button "Create new course"
    current_path.should eq(courses_path("en"))
    page.should have_content("New course created successfully")
    page.should have_content("course 1")
  end
end

When change it "should create, show, edit and delete a course", js: true do

Other test that where working before start to not pass and as pages don't seem to go to the right place when clicking links (going to root instead) and this test doesn't pass giving: ActionController::RoutingError: No route matches [GET] "/fonts/fontawesome-webfont.woff"

InesM
  • 331
  • 4
  • 16

3 Answers3

1

To achieve what you want, you'll need capybara, poltergeist, and phantomJS. When installing, poltergeist will download phantomJS automatically.

Relevant : http://www.railsonmaui.com/tips/rails/capybara-phantomjs-poltergeist-rspec-rails-tips.html

Fred
  • 1,607
  • 20
  • 33
  • This setup, described in the article, works for sure. I'm using it. Show what you tried – Fred May 20 '16 at 16:21
0

You do not necessarily need to install phantomJS as suggested by Fred. In fact I would recommend using selenium as you driver if you can but Fred is correct in that you will need capybara. https://github.com/jnicklas/capybara.

notice the js: true option. Let me know if you have questions.

ruby_newbie
  • 3,190
  • 3
  • 18
  • 29
  • Yes, it works too with selenium. It's fun to see the browser opening and doing stuff automatically. But it's a matter of speed and taste. – Fred May 20 '16 at 16:23
  • I'm already using capybara. I also tried to use the js: true option but then I'm getting `ActionController::RoutingError: No route matches [GET] "/fonts/fontawesome-webfont.woff"` – InesM May 20 '16 at 16:23
  • http://stackoverflow.com/questions/14629491/capybara-tests-with-js-true-routing-error-no-route-matches-get-assets As far as the selenium vs phantom issue I prefer selenium because it is more robust IME. That said, selenium doesn't work with every JS framework and Phantom is definitely faster though as Fred mentioned. – ruby_newbie May 20 '16 at 16:28
0

As Fred suggested I needed to use poltergeist to acheieve this but still I was running into some errors. Mainly FactoryGirl stopped working after I enable javascript in my test.

This approach solved all the problems for me: Capybara + Poltergeist with Database-cleaner not finding FactoryGirl records

Basically I needed to delete my rails_helper.rb for this to work and only use spec_helper.rb.

The final spec_helper.rb file looked like this:

require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'database_cleaner'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist' 

require 'support/mailer_macros'
require 'support/test_helper'

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new app, window_size: [1600, 1200], js_errors: false
end

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.include(MailerMacros)
  config.include(TestHelper)
  config.before(:each) { reset_email }

  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

  Capybara.javascript_driver = :poltergeist
  config.include Capybara::DSL

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation) # moving to before :each doesn't help
    DatabaseCleaner.strategy = :truncation # moving to before :each doesn't help
  end

  config.around :each do |example| # refactoring as before/after with .start/.clean doesn't help
    DatabaseCleaner.cleaning { example.run }
  end
end
Community
  • 1
  • 1
InesM
  • 331
  • 4
  • 16