Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec and javascript

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"

like image 564
InesM Avatar asked Sep 14 '26 19:09

InesM


1 Answers

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

like image 64
Fred Avatar answered Sep 17 '26 09:09

Fred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!