tiloman
tiloman

Reputation: 85

Fixtures for Apartment Gem

I'm trying to do some tests with my multi-tenancy rails app but getting into trouble with my fixtures. In my controller test I have the following setup:

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

When running the test I get this Error:

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

I think the problem is that the fixtures are being created before switching to the test tenant. How do I create the fixtures after switching the tenant?

Upvotes: 1

Views: 319

Answers (1)

craig.kaminsky
craig.kaminsky

Reputation: 5598

Ran into this same problem this morning ... I was able to get around it, at least for now, with this change in my test_helper.rb file:

# 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

Basically, I moved the tenant creation outside the setup method, where it previously resided. This seems to have fixed the issue with the fixtures being created outside the tenant on which I am testing.

I hope this helps ... not 100% sure it's ideal but it does have my tests running today :)

Upvotes: 0

Related Questions