Here is my request spec:
require 'spec_helper'
describe "user signs in", focus: true do
before(:each) do
create(:account_type)
end
it "allows users to sign in with their email address and password" do
test_user = build_stubbed(:user)
visit root_path
click_link "Sign-In"
fill_in "user_email", with: test_user.email
fill_in "user_password", with: test_user.password
click_button "Sign In"
page.should have_content "Vitals"
end
end
The issue is that I can see the request happen to log the user in, but when the user is logged in, the request is redirected back to the home page without an error message.
Here is my user factory:
FactoryGirl.define do
factory :user do
email Faker::Internet.email
password "password"
password_confirmation "password"
agreed_to_age_requirements true
subscriptions {[create(:subscription)]}
end
end
subscriptions factory:
FactoryGirl.define do
factory :subscription do
account_type_id 1
trial_expiry 30.days.from_now
active true
end
end
I am not sure why the request is failing and not signing-in the user.
Related
i'm new at rails and i'm testing my devise gem User with capybara rspec and factory girl.
Here's spec code:
require 'rails_helper'
RSpec.describe User, type: :request do
it "displays the user's email after successful login" do
user = FactoryGirl.create(:user, email: 'test#test.com', password: 'password')
visit root_url
fill_in 'Email', with: 'test#test.com'
fill_in 'Password', with: 'password'
click_button 'Log in'
expect page.has_content?('jdoe')
end
end
The problem is in
expect page.has_content?('jdoe')
No matter what i put instead 'jdoe' test works perfectly without any errors.
Here's factory:
FactoryGirl.define do
factory :user do
email 'test#test.com'
password 'password'
password_confirmation 'password'
end
end
Maybe I missed something or going with a wrong way. How should I test here?
# spec/features/sessions_spec.rb
require 'rails_helper'
RSpec.feature "Sessions" do
scenario "displays the user's email after successful login" do
user = FactoryGirl.create(:user)
visit root_url
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log in'
expect(page).to have_content("Signed in successfully")
# will give a false postitive if form is rerended?
expect(page).to have_content(user.email)
end
end
A few things here:
Use a feature and not a request spec for end to end testing.
Don't hardcode object attributes. The whole point of factories is that the factory takes care of generating unique data so that the tests don't give false positives due to residual state.
Use expect(page).to instead of expect page.should. expect(page).to sets a subject and uses a RSpec matcher which will show you the text of the page in an error message if it does not match.
I have two Factory Girl objects on a single file.
spec/factories/users.rb
FactoryGirl.define do
factory :user do
first_name "Charlie"
last_name "Brown"
email "email#example.com"
password "charbar1234"
password_confirmation "charbar1234"
end
factory :admin do
first_name "Bob"
last_name "Marley"
email "bob#marley.com"
password "bobmarley1234"
password_confirmation "bobmarley1234"
admin true
end
end
When I call create(:user), my test runs fine. When I call create(:admin), I get the following error...
Failures:
1) admin accesses the database
Failure/Error: create(:admin)
NameError:
uninitialized constant Admin
# ./spec/features/admins/admin_spec.rb:5:in `block (2 levels) in <top (required)>'
Here is the test..
spec/features/admins/admin_spec.rb
require "rails_helper"
describe "admin" do
it 'accesses the database' do
create(:admin)
visit root_path
click_link "Log In"
fill_in "Email", with: "bob#marley.com"
fill_in "Password", with: "bobmarley1234"
click_button "Log In"
expect(current_path).to eq(admin_dashboard_path)
with 'h1' do
expect(page).to have_content 'Administration'
end
expect(page).to have_content 'Manage Users'
expect(page).to have_content 'Manage Articles'
end
end
You need to put the factory :admin inside of the factory :user.
It should look something like this:
FactoryGirl.define do
factory :user do
...
factory :admin do
admin true
end
end
end
See Factorygirl Admin Creation
i'm run into the problem with testing user sign in procces
here my test
require 'spec_helper'
include Warden::Test::Helpers
include Devise::TestHelpers
describe "UserSignin" do
it "should allow a registered user to sign in" do
user = FactoryGirl.create(:employer, :email => "email#email.com")
user.confirm!
visit "/users/sign_in"
fill_in "Email", :with => user.email
fill_in "Password", :with => "12345678"
click_button "Sign in"
current_path.should == '/employer'
expect(page).to have_content('My Account')
end
end
if it's improtant i'm using device for authentication
in factory i also have :employer factory
problem with authenticate
after clicking sign_in i'v got an error invalid email or password
UPDATE
there was problem with configs for test environment in spec helper
the solution is to set:
DatabaseCleaner.strategy = :truncation
I have two capybara tests that are testing the signup process on my rails application, both using factory girl. One is just using Factory Girl build command and saving it with the form:
it 'should create a user and associated customer_info', js: true do
visit signup_path
user = build(:user)
customer = build(:customer_info)
sign_up user, customer
page.should have_content 'Welcome back, ' + customer.firstname
end
Whereas the other is using the create command, and then attempting to sign in with that info.
it 'should be able to sign in', js: true do
user = create(:user)
customer = create(:customer_info, user_id: user.id)
visit new_user_session_path
fill_in 'user_email', with: user.email
fill_in 'user_password', with: user.password
click_button 'Sign in'
page.should have_content 'Welcome back, ' + customer.firstname
end
The first one passes and saves in my test database. The second one fails, saying "invalid email or password," but also when I check my database after each test, the first one saves a record but the second one doesn't (which I'm assuming is why it's saying invalid email/password).
Any ideas why my FactoryGirl create function isn't actually saving my record in the database?
EDIT
I have a sequence in my FactoryGirl definition for the email, and build AND create both increase the sequence, so it shouldn't be creating dups, right?
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "foo#{n}#example.com"}
password "secret"
password_confirmation "secret"
end
end
The problem is you are trying to create duplicate user. Sign up creates user in test database and now when you are trying to create a new user with FactoryGirl it would raise validation error because the same user is already there in test database. You should do something like this:
def create_user
#user ||= create(:user)
end
it 'should create a user and associated customer_info', js: true do
visit signup_path
#user = build(:user)
customer = build(:customer_info)
sign_up #user, customer
page.should have_content 'Welcome, ' + customer.firstname
end
it 'should be able to sign in', js: true do
create_user
customer = create(:customer_info, user_id: #user.id)
visit new_user_session_path
fill_in 'user_email', with: #user.email
fill_in 'user_password', with: #user.password
click_button 'Sign in'
page.should have_content 'Welcome back, ' + customer.firstname
end
May be you can use the different approach to solve it. but main focus is on to use the single user object for sign up and sign in.
Hope this would help you.
I have the following error: "Validation failed: Email address is already used" while trying to run feature for Devise user signing in.
I suspect the problem is in Factory generated user, which somehow being created twice with the same email address. I tried to run rake db:test:prepare in order to reset test database, unfortunately, with no results.
my user_factory.rb
Factory.define :user do |user|
user.email "user_#{rand(1000).to_s}#example.com"
user.password "password"
user.confirmed_at nil
end
Steps, which are failing with validation error are "Then I should be registered and signed in" and "And user signs in". my signing_in.feature
Feature: Signing in
In order to use the site
As a user
I want to be able to sign in
Scenario: Signing in via confirmation
Given there is an unconfirmed user
And I open the email with subject "Confirmation instructions"
And they click the first link in the email
Then I should be registered and signed in
Scenario: Signing in via form
Given there is a confirmed user
And I am on the homepage
And user signs in
Then I should see "Signed in successfully"
my user_steps.rb
def unconfirmed_user
#unconfirmed_user = Factory(:user)
end
def sign_up user
visit '/users/sign_up'
fill_in('Email', :with => "unique#unique.com")
fill_in('Password', :with => user.password)
fill_in('Password confirmation', :with => user.password)
click_button('Sign up')
end
def sign_in user
visit 'users/sign_in'
fill_in('Email', :with => user.email)
fill_in('Password', :with => user.password)
click_button('Sign in')
end
When /^I'm signing up$/ do
sign_up unconfirmed_user
end
Given /^there is an unconfirmed user$/ do
unconfirmed_user
end
Given /^there is a confirmed user$/ do
unconfirmed_user.confirm!
end
Then /^I should be registered and signed in$/ do
page.should have_content("Signed in as #{unconfirmed_user.email}")
end
Given /^user signs in$/ do
sign_in unconfirmed_user.confirm!
end
When /^I open the email with subject "([^"]*?)"$/ do |subject|
open_email(#unconfirmed_user.email, :with_subject => subject)
end
What it can be?
Thanks in advance for your help.
You should be clearing the database between tests, mostly using database_cleaner. Check the manual because configuration varies between setups. We are using this one (in env.rb):
DatabaseCleaner.strategy = :truncation
Before do
DatabaseCleaner.clean
end
Also, you should remove the rand directive in the factories file and use the goodies FactoryGirl provides:
u.sequence(:email){|n| "user_#{n}#example.com" }