I’m still getting familiar w/ RSpec and am running into some issue when setting up test for my multi tenant app.
The app works at:
client1.example.com
client2.example.com
etc….
In my RSpec i’m doing the following:
let(:tenant) { FactoryGirl.create(:tenant, subdomain: "client1") }
let(:root_path) { "http://client1.example.dev:3000" }
before {
tenant.save
visit root_path
}
describe "header" do
it "should have the right title" do
page.should have_selector('title', :text => tenant.name)
end
end
I’m doing a few things that feel wrong here, but not sure what the best approach is.
-
I’m hard coding the root_path. Doing something like
visit '/'doesn’t work as the Test doesn’t know what the correct subdomain is. Is this OK? -
I’m not sure why, but I am having to do tenant.save before each test in order for the test to actually be able to find the tenant based on the subdomain. If I remove tenant.save I get a
Couldn't find Tenant with subdomain = client1error. I thought FactoryGirl.create actually saved to the database?
Thanks for the help!
1: Seems like this would be necessary, I don’t think there is a way to specify only the subdomain.
2: Yes,
createsaves to the database (whilebuilddoes not). However, RSpec’sletis lazy – it only evaluates when it is first called.That means without the
tenant.savethe tenant wouldn’t be created untilpage.should have_selector('title', :text => tenant.name)happened – after thevisitcall, so too late.Fortunately, RSpec also provides
let!which is an eager version oflet, so if you use that you won’t need thetenant.savein thebeforeblock.