Testing Your Application

Harald Martin Bråttbåkk edited this page Jan 27, 2016 · 3 revisions

Here are a few tips to help you get over the hurdles of testing a multi-tenant application.

We use Rspec at Influitive, I'm assuming there are similar facilities for test/unit or mini-test (ie setup/teardown), if anyone wants to contribute the equivalent that'd be great.

The number one thing that has helped us in testing is to create a single tenant before the whole test suite runs and switch to that tenant for the run of your tests.

TL;DR You want to ensure your db is clean at the start of the suite and that a tenant exists that you can create your fixture/factory models within. Before each test runs you should switch into that tenant so that way you can guarantee a model to exist for that tenant at the time that the test runs

Our spec_helper.rb looks like so:

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')

  config.before(:each) do
    # Start transaction for this test
    # Switch into the default tenant
    Apartment::Tenant.switch! 'app'

  config.after(:each) do
    # Reset tentant back to `public`
    # Rollback transaction

Note that we use a Company object as our tenant model for storing each tenant and any associated config with it. This Company model has an after_commit that actually creates an Apartment tenant based on the subdomain.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.