I'm trying to test devise sign in, sign out and all the other scenarios, however I cannot get a single scenario to past, lets take login failure
in my feature I have
scenario 'user cannot sign in if not registered' do
login_as('user2#example.com', 'meow')
visit overviews_path
save_and_open_page
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'email'
end
I also have the sign_in helper setup as;
def sign_in(email, password)
user.confirm!
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'password', with: password
click_button 'Log in'
end
however this create an error;
expected to find text "Invalid email or password." in "TypeError at /overviews ======================= > no implicit conversion of Symbol into Integer spec/features/users/sign_in_spec.rb, line 14
any ideas?
You named your helper method sign_in but you're calling login_as in your scenario. You should use one approach or the other, not both.
UPDATE: OK, rechecked the documentation, and you should either use your own helper so that you're emulating an actual user signing in, or the login_as provided by Warden, in which case make sure you're included this in your tests / rspec setup:
include Warden::Test::Helpers
Warden.test_mode!
On a side note, you should confirm your user in your factory/fixture, not in your helper method (where it's probably not defined).
Related
My devise user is :confirmable, and I want to test that the user is redirected to the devise login page after signing up. It works when I manually test it, but fails in rspec/capybara feature spec. I'm following these instructions to setup the redirect.
# registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(_resource)
new_user_session_path # redirect to login
end
end
# registration_spec.rb
RSpec.describe 'registration process', type: feature do
it 'works' do
visit new_user_registration_path
fill_in 'Username', with: 'user123'
fill_in 'Email', with: 'user123#email.com'
fill_in 'Password', with: 'password123'
fill_in 'Password confirmation', with: 'password123'
click_on 'Sign up'
expect(User.find_by(username: 'user123')).not_to be_nil # sanity check
expect(page).to have_current_path(new_user_session_path) # failure here
end
end
Failure returned:
Failure/Error: expect(page).to have_current_path(new_user_session_path)
expected "/users" to equal "/users/sign_in"
# ./spec/features/registration_spec.rb:19:in `block (2 levels) in <top (required)>'
It seems like page is stuck in the post path, or is being redirected to /users incorrectly? I know the form is accepting the input because of my sanity check that the user exists.
Most likely your user creation is failing, whether that's due to an existing user or the password not meeting minimum security requirements, etc I'm not sure. Outputting page.html after the click should show you any validation errors, or your test.log should show them too.
Additionally, click_on can be asynchronous (when using a JS capable driver) so you should swap the last two lines of your test, because have_current_path will wait for the page change to occur which implies the user creation has completed. That won't fix your current issue, but using the Capybara matchers to make sure actions have completed before running direct database queries will reduce potential flakiness for you in the future (Note, however, that direct database queries are generally a bad code smell in features tests).
I am trying to test what happens after logging in through Devise gem. For example, I have the controller to go to student_dashboard_path after users successfully login.
How can I test this with Capybara and Rspec?
I currently have this in:
/spec/features/user_signs_in_sees_dashboard_spec.rb
require 'rails_helper'
feature 'User sign in' do
scenario 'successfully from sign in page and sees student dashboard' do
sign_in
visit student_dashboard_path
expect(current_path).to eq(student_dashboard_path)
end
end
and I have this in:
/spec/support/features/sign_in.rb
module Features
def sign_in
visit user_session_path
fill_in 'Email', with: User.first.email
fill_in 'Password', with: User.first.password
click_button 'Log in'
end
end
and I am getting this error message:
1) User signs in successfully from sign in and sees dashboard
Failure/Error: expect(current_path).to eq(student_dashboard_path)
expected: "/student/dashboard"
got: "/student/login"
I am not sure why I am not able to log in and see the student dashboard.
I'm leaving my original answer below for anyone still on Capybara < 2.5.0 but in 2.5.0 you can now do
expect(page).to have_current_path(<expected path>)
and it will use Capybara's waiting behavior while checking for the path
---- Below is only for Capybara version < 2.5.0
expect(current_path).to eq(...) doesn't wait for the path to change, it just compares to the current path at the time it's called. If you put a sleep after the click button I bet it works. A better solution would be to have something like
expect(page).to have_text('You are now logged in')
after the click_button. That would cause capybara to wait until the log in has completed, the page loads (and the logged in notice appears), and therefore until the current_path has changed too.
Use capybara save_and_open_page in middle to figure out if the fields are properly set. If you work on the same machine you can switch to selenium to test out on the real browser.
Also be sure that this code does not need any JS to work because default capybara matchers will not be able to run it.
I'm using rspec, capybara and Selenium to test my whole application stack. I've turned off transactional fixtures, and I'm using database cleaner to clean my database only after the whole suite has been run. These allows me to test things based using objects created in preceding tests.
Anyway, let's say I want to create user a999 (via a form, so a test in itself) and then proceed to test logging him out and logging him back in.
def sign_up(first_name, last_name, profile_name, email, password)
visit "/"
click_link "Register"
fill_in('First name', with: first_name)
fill_in('Last name', with: last_name)
fill_in('Profile name', with: profile_name)
fill_in('Email', with: email)
fill_in('Password', with: password)
fill_in('Password confirmation', with: password)
click_button 'Sign up'
end
feature "user a999 sign up", js: true do
before(:each){
sign_up( #a999.first_name, #a999.last_name, #a999.profile_name, #a999.email, #a999.password )
}
scenario "welcome message" do
expect(page).to have_content ("Welcome," + #a999.first_name)
end
scenario "can log out" do
end
scenario "can log in" do
end
end
The code above almost works. This is what happens when it's run:
The before block signs up the user before the "welcome message" expectation (I see it physically happening in Firefox thanks to Selenium), and then the welcome message appears after a redirect so the "welcome message" spec passes.
However, because I have the before block set to 'each' the before block is run another two times, meaning I now have three a999 users in the database.
Of course, and setting the before block to (:all) should fix this problem. The user is signed up one, and we go from there, signing the exact same user in and out. It's a feature test that tests the whole stack remember, so I want to do this properly, emulate how a real user will be using my app.
def sign_up(first_name, last_name, profile_name, email, password)
visit "/"
click_link "Register"
fill_in('First name', with: first_name)
fill_in('Last name', with: last_name)
fill_in('Profile name', with: profile_name)
fill_in('Email', with: email)
fill_in('Password', with: password)
fill_in('Password confirmation', with: password)
click_button 'Sign up'
end
feature "user a999 sign up", js: true do
before(:all){
sign_up( #a999.first_name, #a999.last_name, #a999.profile_name, #a999.email, #a999.password )
}
scenario "welcome message" do
expect(page).to have_content ("Welcome," + #a999.first_name)
end
scenario "can log out" do
end
scenario "can log in" do
end
end
But with this code nothing happens at all. Seriously, just nothing. Selenium doesn't follow the code in the before block at all! Firefox doesn't even start up.
Why is this? I mean, that should work at the very least.
before(:each) = signs user up before my eyes
before(:all) = completely dead
I can't explain why nothing comes up at all, but based on numerous posts*, you can't reasonable use before(:all) with capybara, since it resets the session between each example.
*Related posts:
Capybara and before(:all) in rspec
capybara/selenium with rspec before :all hook
I am working on tests but running in to a road block on pages that require a current_user. I am using minitest, capybara, factorygirl, and authlogic, in rails 3.2.9 with ruby 1.9.3p327. I installed minitest as a separate gem, and seem to have the test environment working correctly.
I have a factory that creates a valid user...I call that factory from in a test like this:
describe "UsersAcceptanceTest" do
it "must load and include content" do
FactoryGirl.create(:user)
visit users_path
page.must_have_content("cPanel")
end
end
The FAIL is correct in informing me that the content "cPanel" was not found (cPanel is a link available to logged in users). The fail error goes on to alert me that it was not found in "log in, forgot password, contact" ... which of course means that the test routed correctly to users_path, but was redirected by authlogic because the user is not logged in. Users cant create themselves in my system and therefor are not auto-logged in on create.
How to I also get the factory to create a new user session with the newly created user?
You can do it this way:
visit signin_path
fill_in 'email', with: user.email
fill_in 'password', with: user.password
click_button "Log in"
Just edit it according to your login page structure.
I don't know about minitest, but in rspec I'd create the separate method with this codedef sign_in...end and put it to support\utilities.rb.
Then your code would be looking like that:
describe "UsersAcceptanceTest" do
let(:user) { Factory(:user) }
subject { page }
it "must load and include content" do
sign_in user
visit users_path
it { should have_link("cPanel", href: cpanel_path) }
end
end
As you can see I've edited your code a little bit more.
I am using Rails 3.1.0 and the recaptcha gem from here. I was running a cucumber test that checks that users can sign up. Sign up requires users to fill out the captcha. I know that my test does not touch the captcha:
When /^I create a new account with email: "(.*?)" and password: "(.*?)"$/ do |email, pw|
click_link "Sign up"
fill_in "Email", :with => email
fill_in "Password", :with => pw
fill_in "Password confirmation", :with => pw
click_button "Sign up"
end
But the test still passes. I check success by verifying that the successful sign up message is present on the page and the recaptcha failure message is not present using this step:
Then /^I should (not )?see "(.*)"$/ do |negate, text|
if negate
page.should_not have_content text
else
page.should have_content text
end
end
The controller is almost identical to the first one suggested here.
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
build_resource
clean_up_passwords(resource)
flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
flash.delete :recaptcha_error
render :new
end
end
end
Is there any reason why recaptcha would not work in the test environment? It seems to work fine in development.
Recaptcha by default does not verify the captcha in the test and cucumber environments (see verify logic, configuration logic, and default value ). If it weren't for this, either testing would be very difficult or the captcha wouldn't be very useful.
Just to add to Bens answer and give a potential workaround:
To make sure things don't work (it "fails with recaptcha") in RSpec I've done
before do
Recaptcha.configuration.skip_verify_env.delete('test')
end
after do
Recaptcha.configuration.skip_verify_env << 'test'
end
Thanks Ben