alv_721
alv_721

Reputation: 325

Rspec/Capybara`js: true` issue

I use Rspec/Capybara in my Rails app. Within one feature test I have js: true option to check for modal and click button in it. Was working fine until the moment I added apartment gem for multi-tenancy and PosgreSQL schemas.

Now it is not finding button for calling modal. I am event not sure It is logged properly (binding.pry not working in tests with js correctly). I thought this is DatabaseCleaner issue, but my config seems to be correct (see below).

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
 end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    Apartment::Tenant.switch!
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
    Apartment::Tenant.reset
  end
end

What options could be suggested to fix this issue?

Upvotes: 2

Views: 547

Answers (2)

alv_721
alv_721

Reputation: 325

Figured out the real reason of the js: true issue. The thing is that I had config.block_unknown_url in my "capybara.rb". For testing multi-tenancy I use lvh.me domain, so I needed to add config.allow_url("*.lvh.me") to "capybara.rb" file to fix broken js: true stuff.

Upvotes: 1

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

Here's an example of RSpec configuration for Apartment. Please pay attention to the part where the schema is dropped.

RSpec.configure do |config|
  config.before(:suite) do
    # Clean all tables to start
    DatabaseCleaner.clean_with :truncation
    # Use transactions for tests
    DatabaseCleaner.strategy = :transaction
    # Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
    Apartment::Tenant.drop('app') rescue nil
    # Create the default tenant for our tests
    Company.create!(name: 'Influitive Corp.', subdomain: 'app')
  end

  config.before(:each) do
    # Start transaction for this test
    DatabaseCleaner.start
    # Switch into the default tenant
    Apartment::Tenant.switch! 'app'
  end

  config.after(:each) do
    # Reset tentant back to `public`
    Apartment::Tenant.reset
    # Rollback transaction
    DatabaseCleaner.clean
  end
end

Also you can rewrite a portion of DatabaseCleaner configuration to be a bit shorter

config.before(:each) do |example|
  DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
  DatabaseCleaner.start
  ...
end

Upvotes: 1

Related Questions