1

We have a multidomain application, where each site can have multiple domains (for the locales)

ApplicationController is doing

def load_site
  @site = Site.find_by!("domains like '%#{request.host}%'")
end

In my test suite i just want to make sure, that the website is showing up

spec/features/user_visits_homepage_spec.rb

require "rails_helper"

feature "User visits Homepage" do
  site = FactoryGirl.create :site
  scenario "successfully", js: true do
    visit root_path
    expect(page).to have_css "h1", text: site.name
  end
end

my database_cleaner.rb

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

Question

how can i assure that i always have 1 Site since without a site no controller will work.

When i create the site as i did, the database_cleaner will always kill it.

Do i have a wrong understanding or do i miss something?

Community
  • 1
  • 1
Tim Kretschmer
  • 2,272
  • 1
  • 22
  • 35

1 Answers1

1

Your database cleaner has two entries relevant to your question, the first two.

Before the entire suite, you clean the whole database by truncating it. Then, each test is wrapped in a transaction. This will result in reverting to the initial state after each test. To set up some persistent data, just do it right after your initial clean, like this:

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    # Add setup data here which will persist through all tests (unless deleted or modified)
  end

Be careful not to use this data in all of your tests. This should just be used to establish a minimum skeleton for your app to function. For example, create a site there, but when testing the CRUD operations of site administation, do it on a seperate site.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • i thought about that, but didn't do. that works. great. another question: lets say i want to have 5 test-users always present (for having tests with filtering the search), is that also the place to put those users in ? – Tim Kretschmer Sep 25 '15 at 05:05
  • @huanson Yep, anything you want for the basic operation of the site. Just be conscious of their presence with the rest of your tests – Brad Werth Sep 25 '15 at 05:07
  • so then i change my test to expect `expect(page).to have_css "h1", text: Site.first.name` since there is no `site` or `@site` in my test left. thanks, working good :-) – Tim Kretschmer Sep 25 '15 at 05:07
  • Brad, is it possible that my logic sucks? Test only green if `js: true` i wonder if my applicationcontroller doesn't have a request.host if its not coming with selenium ? – Tim Kretschmer Sep 25 '15 at 05:22
  • @huanson Not sucks, but these are notoriously hard to get dialed in just right. I've had the best luck with something like http://stackoverflow.com/a/22941859/525478 but YMMV. – Brad Werth Sep 25 '15 at 05:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90581/discussion-between-huan-son-and-brad-werth). – Tim Kretschmer Sep 25 '15 at 05:31