【问题标题】:Fixtures for Apartment Gem公寓宝石的固定装置
【发布时间】:2020-04-28 21:31:54
【问题描述】:

我正在尝试使用我的多租户 Rails 应用程序进行一些测试,但我的固定装置遇到了麻烦。在我的控制器测试中,我有以下设置:

setup do
  Apartment::Tenant.create('test')
  Apartment::Tenant.switch! 'test'
  host! "test:3000"
  sign_in users(:admin)
end

运行测试时出现此错误:

ActiveRecord::RecordNotFound: Couldn't find User with 'id'=255947101

我认为问题在于在切换到测试租户之前创建了固定装置。切换租户后如何创建灯具?

【问题讨论】:

    标签: ruby-on-rails multi-tenant apartment-gem


    【解决方案1】:

    今天早上遇到了同样的问题......至少现在我能够解决它,因为我的 test_helper.rb 文件中的这个更改:

    # Customizing the base TestCase
    module ActiveSupport
      class TestCase
        Apartment::Tenant.create('the-tenant')
        Apartment::Tenant.switch! 'the-tenant'
        fixtures :all
    
        ActiveRecord::Migration.check_pending!
    
        def setup
          Capybara.app_host = 'http://www.my-somain.local'
          DatabaseCleaner.strategy = :transaction
        end
    
        def teardown
          Apartment::Tenant.drop('the-tenant')
          Apartment::Tenant.reset
          DatabaseCleaner.clean
        end
      end
    end
    

    基本上,我将租户创建移到了它之前所在的 setup 方法之外。这似乎解决了在我正在测试的租户之外创建的固定装置的问题。

    我希望这会有所帮助......不是 100% 确定它是理想的,但它确实让我的测试在今天运行 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多