5

Javascript を使用すると、リクエスト スペックが機能しません。私の仕様は、Javascript なしで実行するとパスします (ページは JS の有無にかかわらず動作するように構築されています)。

具体的には、 のようなアサーションを行うと仕様が失敗しPost.should have(1).recordます。Capybara は DB からレコードを取得しないだけで、データベースは実行間でクリーンアップされません。

トランザクションフィクスチャを無効にして DatabaseCleaner を使用してみました-これに対する一般的なアプローチだと思います。サイコロはありません。

また、DatabaseCleaner を使用せずに実行し、トランザクション フィクスチャを使用し、AR にスレッド間の同じ接続を強制的に共有させようとしました ( José Valim が説明したパッチ)。繰り返しますが、サイコロはありません。

さらに、Capybara-webkit と Selenium を切り替えてみましたが、問題は解決しません。

問題を再現する基本的なPostスキャフォールドのみを含むサンプル アプリを 作成しました。他のシナリオの場合は rb です。

私は通常 Spork を使用しますが、両方の spec_helper.rb ファイルで無効にしました。これは、潜在的な障害点を排除するためです (「実際の」アプリとサンプル アプリの両方で)。

私は RVM を介して MRI 1.9.3 で OS X 10.7.3 を実行している Macbook Air で Pow を使用してローカルで開発しています。(1.9.2も試しました)。

私が理にかなっていることを願っています-ガイダンス/ヘルプ/ポインターは歓迎です!

4

3 に答える 3

3

マット - 私を助けてくれてありがとう!Seleniumをjavascriptドライバーとして使用して、spec_helperで設定してみました。

仕様はまだ失敗しました-しかし、Firefoxで正しい動作が実行されているのを見ることができました...そして、CapybaraがAJAXリクエストの終了を待たないために問題が発生する可能性があることに気づきました。

次に、最初の spec_helper (Spork を使用し、DatabaseCleaner を使用しない) に戻し、単純に Capybara のwait_until { page.has_content? "text I'm inserting with JS" }.

サンプル アプリを更新sleep 1し、リクエスト仕様に追加しただけなので、ご自身で確認してください。Spork の有無にかかわらず動作するようになり、AR モンキー パッチは完全に動作するようです。

于 2012-04-03T22:42:44.250 に答える
1

spec_helper.rb以下にリストされているコードを試してみましたが、テストは成功しました。データベース クリーナーをトリガーするための構文は、spec_helper_database_cleaner.rb.

私たちはこれを本番環境で使用しており、Jose Valim によって提案された変更も試みましたが、うまくいきませんでした。

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  # Add this to load Capybara integration:
  require 'capybara/rspec'
  require 'capybara/rails'

  include Capybara::DSL

  # 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}

  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

    # 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

    # Include sign_in & sign_out for tests
    # config.include Devise::TestHelpers, :type => :controller

    # Use database_cleaner to ensure a known good test db state as we can't use
    # transactional fixures due to selenium testing
    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
end
于 2012-04-03T13:24:35.970 に答える
0

Joséの提案は私には有効でしたが、Sporkを使用したときは有効ではありませんでした。しかし、これを追加しましspec_helper.rbた:

Spork.prefork do
  RSpec.configure do |config|
    # Make it so poltergeist (out of thread) tests can work with transactional fixtures
    # http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846
    ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
      def current_connection_id
        Thread.main.object_id
      end
    end
  end
end

出典:http ://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846

于 2013-03-06T19:20:39.167 に答える