I have the following spec:
# MUST BE TESTED WITH JAVASCRIPT. As you can see before, it is allowing
# the Log Out link. On a real page, that element is hidden.
it "logs out a user" do
user = login_user
click_link user.email
click_link "Log Out"
assert page.has_content?('Logged out')
assert page.has_content?('Company Profile')
end
My login_user
method is in my spec_helper.rb
file and is as follows:
def login_user(admin = false)
user = FactoryGirl.create(:user, :admin => admin)
visit login_path
fill_in 'Email', :with => user.email
fill_in 'Password', :with => user.password
click_button 'Go'
assert page.has_content?('Logged in')
user
end
That last assert
fails when I add , js: true
to my spec.
And when I do a save_and_open_page
right before it, Up comes the page with zero CSS and presumably no JS either since. It's not reading the assets for some reason. I thought that when you ran tests, it would read the assets just like it does in development mode. I really need help.
I found the answer in this question. This was EXTREMELY tricky. I still don't know exactly why this failed in the first place, but needless to say with RSpec/Capybara/PhantomJS/Poltergeist/Guard/Spork/FactoryGirl
there are a LOT of moving parts.
Please follow the link to see the answer. It has to to with transactional fixtures and enabling the DatabaseCleaner
gem. It doesn't do SO any good to state the answer twice, I will leave that one as the canonical answer.
UPDATE
Actually this problem has to do with how database connections are not shared under Poltergeist/PhantomJS. The guys at Plataformatec have a superior solution on their blog which does not involve the use of the DatabaseCleaner gem.
IMPORTANT NOTE
For Postgres users who are also using Spork, the blog solution will not work as stated. In other words, if you put the shared_db_connection.rb
file in your spec/support
directory, you will get a PG::Error connection closed
message. Leave the file in spec/support
but remove this line:
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
And instead put that in your Spork.each_run
block in your spec_helper.rb
file.