Reputation: 79
I am learning RSpec and Capybara and trying to test if a user can navigate to the login page (powered by Devise) and successfully login. The test is not seeing the page it should be after logging in successfully. When using the browser, if input is not present, it returns to the login page. I am using Rails 5.
login_spec.rb
require 'spec_helper'
require 'rails_helper'
RSpec.feature "Logging in a User" do
scenario "Logging in user shows special content" do
visit "/"
click_link "Sign In"
page.should have_content("Password")
#fill in login information
page.fill_in 'Email', with: '[email protected]'
page.fill_in 'Password', with: 'some_password'
click_on 'Log in'
page.should have_no_content("Wait for the text which is available in the sign in page but not on next page")
page.should have_content('User:')
expect(page.current_path).to eq(root_path)
end
end
Capybara error message:
1) Logging in a User Logging in user shows special content
Failure/Error: page.should have_content('User:')
expected to find text "User:" in "Log in\nEmail\nPassword\nRemember me\nSign up Forgot your password?"
# ./spec/features/login_spec.rb:17:in `block (2 levels) in <top (required)>'
Upvotes: 2
Views: 1982
Reputation: 49890
Yes you can check if a field has been filled in with the have_field
matcher
expect(page).to have_field('Email', with: '[email protected]')
will verify the page has a field with a label of 'Email' and a filled in value of '[email protected]'.
It's not the cause of your current issue, but is there a reason you're mixing RSpecs should
and expect
syntaxes? You really should stick to one, preferably `expect for new code - so
expect(page).to have_content("Password")
expect(page).not_to have_content("Wait for the text which is ...
instead of
page.should have_content("Password")
page.should have_no_content("Wait for the text which is ...
Also - you should almost never use plain RSpec matchers (eq
, etc) with anything Capybara related, instead you should use the Capybara provided matchers
expect(page).to have_current_path(root_path)
instead of expect(current_path)...
Upvotes: 2