Capybara with :js => true causes test to fail

后端 未结 3 1129
北荒
北荒 2020-11-28 04:06

I\'m new to Capybara and testing on Rails in general, so please forgive me if this is a simple answer.

I\'ve got this test

it \"should be able to edi         


        
相关标签:
3条回答
  • 2020-11-28 04:14

    A variation of brutuscat's answer that fixed our feature specs (which all use Capybara):

    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end
    
    config.before(:each) do
      # set the default
      DatabaseCleaner.strategy = :transaction
    end
    
    config.before(:each, type: :feature) do
      DatabaseCleaner.strategy = :truncation
    end
    
    config.before(:each) do
      DatabaseCleaner.start
    end
    
    config.append_after(:each) do
      DatabaseCleaner.clean
    end
    
    0 讨论(0)
  • 2020-11-28 04:19

    I've read the Capybara readme at https://github.com/jnicklas/capybara and it solved my issue.

    Transactional fixtures only work in the default Rack::Test driver, but not for other drivers like Selenium. Cucumber takes care of this automatically, but with Test::Unit or RSpec, you may have to use the database_cleaner gem. See this explanation (and code for solution 2 and solution 3) for details.

    But basically its a threading issue that involves Capybara having its own thread when running the non-Rack driver, that makes the transactional fixtures feature to use a second connection in another context. So the driver thread is never in the same context of the running rspec.

    Luckily this can be easily solve (at least it solved for me) doing a dynamic switching in th DatabaseCleaner strategy to use:

    RSpec.configure do |config|
      config.use_transactional_fixtures = false
    
      config.before :each do
        if Capybara.current_driver == :rack_test
          DatabaseCleaner.strategy = :transaction
        else
          DatabaseCleaner.strategy = :truncation
        end
        DatabaseCleaner.start
      end
    
      config.after do
        DatabaseCleaner.clean
      end
    end
    
    0 讨论(0)
  • 2020-11-28 04:25

    There is another way to deal with this problem now described and discussed here: Why not use shared ActiveRecord connections for Rspec + Selenium?

    0 讨论(0)
提交回复
热议问题