2016-01-21 138 views
4

我是Minitest和Apartment的新成員,難以正確配置環境來運行測試用例。我想使用Capybara & Selen進行驗收測試。當我運行我的測試時,我收到以下錯誤消息:如何測試Apartment,Minitest,水豚和硒

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 

回答

2

在測試了一些不同的組合後,我想出了自己的答案。解決方案其實很簡單。所有公寓&水豚相關的配置應該在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 
+0

這種方法是否足夠好,可以加速使用minitest進行軌道測試?我在與minitest測試公寓的過程中度過了一段可怕的時光,因爲每次調用測試方法時都必須創建租戶。 – artificis

+0

我的觀點是,按照你的方法,租戶只在一個測試周期內創建一次?還是在每個測試用例之前創建? – artificis

+0

租戶只創建一次。然後,它將運行所有遷移以爲租戶創建表。 – talakoski

0

公寓寶石維基建議您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

謝謝,我認爲這是有效的,但RSpec的我想用MINITEST,我傻。 – talakoski