How to test apartments, mini-apartments, Capybara & Selenium

I am new to Minitest and Apartment and have difficulty setting up the environment correctly to run test cases. I want to accept acceptance tests using Capybara and Selenium. When I run my tests, I get the following error message:

Apartment::TenantNotFound:         Apartment::TenantNotFound: One of the following schema(s) is invalid: "test-tenant" "public"

So, it seems that the tenant is not created correctly. In the apartment, the gem has instructions on how to use it with Rspec, but I do not know how to make a similar setting in Minitest. How to identify tenants so that Minitest can see them?

My test_helpers.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
require "minitest/rails/capybara"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
end

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActionDispatch::IntegrationTest
end

And a test case:

require "test_helper"

class LoginTest < Capybara::Rails::TestCase
  def setup
    Apartment::Tenant.drop( "test-tenant" ) rescue nil
    Apartment::Tenant.create( "test-tenant" ) rescue nil
    Apartment::Tenant.switch!( "test-tenant" )

    # Since we are using Apartment gem, we need to tell Capybara to connect our testing tenant URL + port number
    Capybara.server_port = 5000
    Capybara.always_include_port = true
    Capybara.app_host = "http://test-tenant.lvh.me"
  end

  feature "Login" do
    scenario "with correct credentials", js: true do
      visit '/accounts/sign_in'
      fill_in("account[email]", with: "#{accounts(:tenant_user).email}")
      fill_in("account[password]", with: "password")
      click_button("Sign in")
      page.must_have_content("Signed in successfully.")

      visit '/'
      page.must_have_content("Welcome")
    end
  end

end
+4
source share
2

. . , Capybara, test_helpers.rb.

test_helpers.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
require "minitest/rails/capybara"
Minitest::Reporters.use!


Apartment::Tenant.drop( "test-tenant" ) rescue nil
Apartment::Tenant.create( "test-tenant" ) rescue nil
Apartment::Tenant.switch!( "test-tenant" )


class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
end

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActionDispatch::IntegrationTest
end

# Since we are using Apartment gem, we need to tell Capybara to connect our testing tenant URL + port number
Capybara.server_port = 5000
Capybara.always_include_port = true
Capybara.app_host = "http://test-tenant.lvh.me"

:

require "test_helper"


class LoginTest < Capybara::Rails::TestCase

  def setup
  end


  feature "Login" do
    scenario "with correct credentials", js: true do
      visit '/accounts/sign_in'
      fill_in("account[email]", with: "#{accounts(:tenant_user).email}")
      fill_in("account[password]", with: "password")
      click_button("Sign in")
      page.must_have_content("Signed in successfully.")

      visit '/'
      page.must_have_content("Welcome")
    end
  end

end
+2

gem wiki spec_helper rails_helper:

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

.

, , , AJAX . Apartment:: TenantNotFound, , JS-.

0

Source: https://habr.com/ru/post/1625314/


All Articles