简体   繁体   中英

RSpec Capybara have_selector Test Not Working

I have a very simple RSpec Capybara have_selector() that doesn't seem to be working and I can't figure out why... but probably something simple.

Here's the RSpec request test:

describe 'with valid information'
  let(:user) { FactoryGirl.create(:user) }
  before do
    fill_in 'Email', with: user.email
    fill_in 'Password, with: user.password
    click_button 'Login'
  end
  it { should have_selector('p', text: user.first_name) }
  ...
end

In the new.html.erb for the session, I just have something like this:

<% if logged_in? %>
  <p><%= current_user.first_name %></p>
<% else %>
  <%= form_for(:session, url: sessions_path) do |f| %>
  ...
<% end %>

The login form session#new works perfectly in the browser. I can login just fine and am able to view the first name in a p tag. The problem seems to be the capybara part, because the other it test checking for the h1 tag on the page (which is present regardless of being logged in) works fine.

Does anyone know what the problem could be?

Thanks in advance for any help!!

More than likely, your login is failing for one reason or another.

Change your test to look like this:

it "should show me my first name" do
  save_and_open_page
  should have_selector('p', text: user.first_name)
end

That will open your browser and show you the page as the test browser currently sees it. (You may need to add the launchy gem to your Gemfile.)

There are a few other diagnostics that would help, for instance, adding a test or a puts that checks what page you landed on, such as:

it "redirects to the home page after log in" do
  current_path.should == root_path
end
fill_in 'Password, with: user.password

not like that. You should use this line,

fill_in 'Password', with: user.password

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM