2012-02-28 10 views
1

しばらくの間、私はcache_classesをfalseにしてSelenium/Spork/Rspecを使用していました。SeleniumからCapybara-Webkitに変更した後でcache_classes/Sporkに関する問題が発生しました

ウェブキットに切り替える際、私はcache_classesに関連するエラーが発生し始めました(たとえば、期待ユーザー、ユーザーがいる)ので、cache_classesをtrueに設定しようとしています。

私は何をすべきかに関係なく、私は次のエラーで終わるしかし:私は物事のすべての種類を試してみました

Capybara::Driver::Webkit::WebkitInvalidResponseError: 
    Unable to load URL: http://127.0.0.1:56398/login 

...を含む:

私は単にcache_classes = falseで生きていて、工場の女の子のエラーを避ける方法を見つけ出すべきか疑問に思っています。どんな助けもありがとう。

require 'spork' 

Spork.prefork do 
    ENV["RAILS_ENV"] ||= 'test' 

    require 'rspec/rails' 
    require 'rspec/autorun' 
    require 'capybara/rails' 
    require 'capybara/rspec' 
    require 'database_cleaner' 
    require 'factory_girl' 
    require 'authlogic/test_case' 
    require 'email_spec' 
    include Authlogic::TestCase 

    require File.expand_path("../../config/environment", __FILE__) 

    RSpec.configure do |config| 
    # == Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 
    config.mock_with :rspec 

    ApplicationController.skip_before_filter :activate_authlogic 

    config.include(EmailSpec::Helpers) 
    config.include(EmailSpec::Matchers) 
    config.include Capybara::DSL 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    #config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = false 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    config.before(:suite) do 
     DatabaseCleaner.strategy = :truncation, {:except => %w[ages coefficients weights1 weights2 weights3 weights4 weights5 weights6 weights7 weights8 etfs]} 
     DatabaseCleaner.clean 
    end 

    config.before(:each) do 
     DatabaseCleaner.start 
     Capybara.current_driver = :webkit if example.metadata[:js] 
     #Capybara.current_driver = :selenium if example.metadata[:js] 
     activate_authlogic 
     ActiveRecord::Base.instantiate_observers 
    end 

    config.after(:each) do 
     DatabaseCleaner.clean 
     Capybara.use_default_driver if example.metadata[:js] 
    end 

    Capybara.default_selector = :css 

    end 

    # Requires supporting ruby files with custom matchers and macros, etc, 
    # in spec/support/ and its subdirectories. 
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    ## SUPPORT METHODS ## 
    ## (erased for clarity) ## 


    ## ## 
    ActiveSupport::Dependencies.clear 
end 

Spork.each_run do 
    #FactoryGirl.reload 

    # Required to fix a recurring error when testing Active_Admin stuff 
    # See here: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-infamous-undefined-local-variable-or-method-view_factory/ 
    # Delete at some point if active admin or whoever fixes this 
    ActionView::Template.register_template_handler :arb, lambda { |template| 
    "self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context" 
    } 

    #ActiveSupport::Dependencies.clear 
end 

UPDATE: SPECは、念のために、それは助け例を追加....あなたはカピバラ-webkitのに問題がスレッドに起因する問題が発生している

describe "Items" do 
    before(:each) do 
    @user = Factory.create(:user) 
    activate_authlogic 
    b = # something not important 
    end 

    describe "usage paths" do 

    it "the form directly from the basic_simulation show page should have correctly functioning javascript validation", :js => true do 
     request_sign_in(@user) # This is a helper method which goes through the login form 
     visit '/basic_simulation' 
     fill_in "amount", :with => "-5000" 
     click_button "Calculate" 
     page.should have_selector("label.jquery-validator.amount-error", :text => "Please enter a value greater than or") 
     fill_in "amount", :with => "5000" 
     click_button "Calculate" 
     page.should have_selector("input#amount", :value => "5000") 
    end 
end 

答えて

3

次のように私の先割れスプーンファイルがありますテストスイートです。

ホセValimは、あなたが彼の勧告に従うならば、あなたはカピバラを使用しながら、テスト中に、あなたのデータの問題を、トランザクションの備品をオンにまとめてデータベース・クリーナを削除し、もはや持っていることができるはずですa recent blog post.

にはるかに明確に説明します-webkit。パフォーマンスのテストでもすばらしい効果が得られます。

しかし、トリックは、ホセの提案がSpork.each_runブロックにあることを確認するか、動作しません。明確にするために、ここに私のspec_helper.rbの関連部分があります。

require 'spork' 

Spork.prefork do 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'capybara/rspec' 

    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    RSpec.configure do |config| 

    config.mock_with :rspec 

    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 

    Capybara.default_driver  = :rack_test 
    Capybara.default_selector = :css 
    Capybara.javascript_driver = :webkit 
    end 
end 

Spork.each_run do 

    if Spork.using_spork? 
    ActiveRecord::Base.instantiate_observers 
    end 

    require 'factory_girl_rails' 

    # Forces all threads to share the same connection, works on Capybara because it starts the web server in a thread. 
    class ActiveRecord::Base 
    mattr_accessor :shared_connection 
    @@shared_connection = nil 

    def self.connection 
     @@shared_connection || retrieve_connection 
    end 
    end 

    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection 
end 

その他いくつかの小さな提案:

  • あなたがfactory_girl_railsの最新バージョンを使用している場合 がSpork.each_run ブロックでrequire factory_girl_railsを使用しなければならないとrequire factory_girlprefork
  • から削除する必要があります
  • 最新のfactory_girl_railsも、もはやActiveSupport::Dependencies.clearを必要としませんが、テストしなければならない人もいますそれを取り除く。
  • 私はまだActiveRecord::Base.instantiate_observersの必要性について確信していませんが、いずれにしても、あなたがオブザーバーを使用していて、それがeach_runブロックにあるはずであると理解している場合にのみ必要です。

すべての機能を試してみてください。

+0

これは、transactional_fixturesをオンに戻すことができると言いました。私の問題はcache_classesを使っているように見えるでしょう...(上記の方法で、use_transactional_fixturesとcache_classesの両方をtrueに設定できますか? – Brandon

+0

"Capybara.javascript_driver =:webkit"を使用している場合、テストでjavascriptを実行する方法をどのように指定しますか? (私は現在:js =>真のビジネスCapybara.current_driver = xxの場合メタデータの場合...) – Brandon

+0

はい。 'test.rb'ファイルで' config.cache_classes = true'を変更することができます。 jsファイルの表示は以前と同じです。これはCapabaraインジケータで、 'spec_helper.rb'で設定された選択されたjsドライバに渡されます。 – nmott

関連する問題