如何测试 Apartment、Minitest、Capybara 和 Selenium
How to test Apartment, Minitest, Capybara & Selenium
我是 Minitest 和 Apartment 的新手,很难正确配置环境以 运行 测试用例。我想使用 Capybara & Selenium 进行验收测试。当我 运行 我的测试时,我收到以下错误消息:
Apartment::TenantNotFound: Apartment::TenantNotFound: One of the following schema(s) is invalid: "test-tenant" "public"
看来租户没有正确创建。 Apartment gem 有关于如何将它与 Rspec 一起使用的说明,但我不知道如何在 Minitest 中进行类似的设置。应如何定义租户以便 Minitest 可以看到他们?
我的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
和测试用例:
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
The Apartment 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 和 selenium 时。然后我遇到了 Apartment::TenantNotFound 错误,即使配置对于没有 JS 格式的测试来说是完美的。
在测试了一些不同的组合后,我自己找到了答案。解决方法其实很简单。所有与 Apartment 和 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
我是 Minitest 和 Apartment 的新手,很难正确配置环境以 运行 测试用例。我想使用 Capybara & Selenium 进行验收测试。当我 运行 我的测试时,我收到以下错误消息:
Apartment::TenantNotFound: Apartment::TenantNotFound: One of the following schema(s) is invalid: "test-tenant" "public"
看来租户没有正确创建。 Apartment gem 有关于如何将它与 Rspec 一起使用的说明,但我不知道如何在 Minitest 中进行类似的设置。应如何定义租户以便 Minitest 可以看到他们?
我的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
和测试用例:
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
The Apartment 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 和 selenium 时。然后我遇到了 Apartment::TenantNotFound 错误,即使配置对于没有 JS 格式的测试来说是完美的。
在测试了一些不同的组合后,我自己找到了答案。解决方法其实很简单。所有与 Apartment 和 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