2016-05-20 10 views
1

私のRubyアプリケーションには、フォームフィールドの塗りつぶしを担当するいくつかのjavascript関数を持つページがあります。問題は、このページのrspecでテストをしようとすると、javascriptが実行されないように見えて、フォームにエラーが発生することです。Rspecとjavascript

この問題を解決する方法はありますか? (フレッドの提案を使用して)

EDITは

マイspec_helper.rbファイルは次のようになります。

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 

そして、私のテストでは、次のようになります。

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 

ときに変更 it "should create, show, edit and delete a course", js: true do

他のテストでは、前に作業が開始されず、リンクがクリックされたときにページが適切な場所に移動しないように見えます(ルートに行く)、このテストでは、次のようなテストが行​​われます:ActionController::RoutingError: No route matches [GET] "/fonts/fontawesome-webfont.woff"

答えて

0

フレッドが示唆したように私はこれを念頭においてポルターガイストを使用する必要があったが、まだいくつかのエラーに遭遇していた。主にFactoryGirlは私のテストでjavascriptを有効にした後に作業を停止しました。

このアプローチ

は私のためにすべての問題を解決:

Capybara + Poltergeist with Database-cleaner not finding FactoryGirl recordsは基本的に私は仕事だけspec_helper.rbを使用するには、このための私rails_helper.rbを削除する必要がありました。

最終spec_helper.rbファイルはこのように見えた:

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 
1

あなたが望むものは、capybarapoltergeist、およびphantomJSが必要です。インストールすると、ポルテジストは自動的にphantomJSをダウンロードします。

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

+0

はまだ動作していないようです:(同じ問題 – InesM

+0

資料に記載このセットアップは、確かに動作します、私はそれを使用しています何を表示します。あなたは試しました – Fred

0

フレッドにより示唆されるようにあなたは、必ずしもphantomJSをインストールする必要はありません。実際、私はあなたができるならばあなたがドライバーとしてセレンを使うことを勧めますが、あなたはカピバラが必要であるという点でフレッドが正しいです。 。

js:trueオプションに注意してください。あなたに質問がある場合はお知らせください。

+0

はい、それもセレンで動作します。ブラウザを開いて物事を自動的に処理するのは楽しいですが、速度と味の問題です – Fred

+0

私はすでにcapybaraを使用しています。しかし、私は 'ActionController :: RoutingError:ルートは[GET]にマッチしません" /fonts/fontawesome-webfont.woff " – InesM

+0

http://stackoverflow.com/questions/14629491/capybara-tests-with-js-真e-routing-error-no-route-matches-get-assetsセレン対ファントムの問題では、セレンはもっと頑丈なIMEであるため、セレンが好きです。つまり、セレンはすべてのJSフレームワークで動作しません。フレッド氏の言葉通り、ファントムは確実に高速です。 –