我有一个多租户应用,它在应用控制器中使用请求子域和默认范围。如果我删除了测试中的js: true
选项,它就能完美运行。但是对于js: true
,似乎before_each
在尝试执行测试之前不会创建tennant。
我是否需要以某种方式添加延迟?有任何想法吗? 这是一些精简代码。
class ApplicationController < ActionController::Base
prepend_around_filter :scope_current_store
protected
def current_store
@store ||= Store.find_by(host: "#{request.subdomain}.#{request.domain}")
end
helper_method :current_store
def scope_current_store
Store.current_id = current_store.id
yield
ensure
Store.current_id
end
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/webkit/matchers'
require 'rspec/retry'
Capybara.automatic_reload = true
Capybara.always_include_port = true
Capybara.app_host = "http://test.lvh.me"
Capybara.javascript_driver = :webkit
Capybara::Webkit.configure do |config|
config.allow_unknown_urls
end
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.order = "random"
config.infer_spec_type_from_file_location!
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fail_fast = true
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if driver_shares_db_connection_with_specs
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
end
config.before :each do
DatabaseCleaner.start
@store = create :store
end
config.after(:each) do
DatabaseCleaner.clean
end
# config.include(Capybara::Webkit::RspecMatchers, :type => :feature)
end
FactoryGirl.define do
factory :store do
name "Test Store"
host "test.lvh.me"
phone '9222'
end
end
it 'clicking a user displays their details', js: true do
@user = create(:user)
visit edit_admin_user_path(@user)
end
1) clicking a user displays their details
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `id' for nil:NilClass
# ./app/controllers/application_controller.rb:31:in `scope_current_store'
Finished in 2.4 seconds (files took 0.25734 seconds to load)
1 example, 1 failure