Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何测试公寓、迷你测试、水豚和;硒_Ruby On Rails_Selenium_Capybara_Minitest_Apartment Gem - Fatal编程技术网

Ruby on rails 如何测试公寓、迷你测试、水豚和;硒

Ruby on rails 如何测试公寓、迷你测试、水豚和;硒,ruby-on-rails,selenium,capybara,minitest,apartment-gem,Ruby On Rails,Selenium,Capybara,Minitest,Apartment Gem,我是Minitest和公寓的新手,很难正确配置环境以运行测试用例。我想使用水豚和硒进行验收测试。当我运行测试时,会收到以下错误消息: Apartment::TenantNotFound: Apartment::TenantNotFound: One of the following schema(s) is invalid: "test-tenant" "public" 所以看起来租户创建得不正确。公寓gem有如何与Rspec一起使用的说明,但我不知道如何在Minitest中进

我是Minitest和公寓的新手,很难正确配置环境以运行测试用例。我想使用水豚和硒进行验收测试。当我运行测试时,会收到以下错误消息:

Apartment::TenantNotFound:         Apartment::TenantNotFound: One of the following schema(s) is invalid: "test-tenant" "public"
所以看起来租户创建得不正确。公寓gem有如何与Rspec一起使用的说明,但我不知道如何在Minitest中进行类似的设置。如何定义租户,以便Minitest能够看到它们

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
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
    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
需要“测试助手”
类LoginTest
公寓gem wiki建议在spec\u助手或rails\u助手中进行以下配置:

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。然后,我遇到了公寓::TenantNotFound错误,即使该配置对于没有JS格式的测试非常有效。

在测试了一些不同的组合后,我自己找到了答案。解决办法其实很简单。应在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!

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
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
需要“测试助手”
类LoginTest
谢谢,我认为这对RSpec是有效的,但我想使用Minitest,愚蠢的我。这种方法足够好用Minitest加速rails测试吗?我在使用minitest测试公寓时经历了一段可怕的时光,因为每次调用测试方法时我都必须创建租户。我的观点是,按照您的方法,一个测试周期只创建一个租户?或者它是在每个测试用例之前创建的?租户只创建一次。然后它将运行所有迁移来为租户创建表。是的,我通过做一些实验来确认。谢谢你的回答