Lumbee Lumbee - 4 months ago 11
Ruby Question

Load an module in an rspec test

I have the following test...

spec/features/users/sign_in_spec.rb



require "rails_helper"
feature "User sign in" do
extend SubdomainHelpers
let!(:account) { FactoryGirl.create(:account) }
let(:sign_in_url) { "http://#{account.subdomain}.example.com/sign_in" }
let(:root_url) { "http://#{account.subdomain}.example.com/" }
within_account_subdomain do
scenario "signs in as an account owner successfully" do
visit root_url
expect(page.current_url).to eq(sign_in_url)
fill_in "Email", :with => account.owner.email
fill_in "Password", :with => "password"
click_button "Sign in"
expect(page).to have_content("You are now signed in.")
expect(page.current_url).to eq(root_url)
end
end
end


Note the 3rd line
extend SubdomainHelpers
which I am trying to load from...

spec/support/subdomain_helpers.rb



module SubdomainHelpers
def within_account_subdomain
let(:subdomain_url) { "http://#{account.subdomain}.example.com" }
before { Capybara.default_host = subdomain_url }
after { Capybara.default_host = "http://www.example.com" }
yield
end
end


When I run the text I get the error
uninitialized constant SubdomainHelpers (NameError)
How do I call the SubdomainHelpers module from the test?

Answer

You need to require 'support/subdomain_helpers' in your rails_helper or the sign_in_spec file.

Files in the spec/support subdirectory are automagically loaded into LOAD_PATH by rspec, but they're not required.

Comments