Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:39:15+00:00 2026-05-23T02:39:15+00:00

I encounter these errors when running the test: 1) UsersController PUT ‘update’ authentication of

  • 0

I encounter these errors when running the test:

  1) UsersController PUT 'update' authentication of edit/update pages for non-signed-in        users should deny access to 'edit'
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:130

  2) UsersController PUT 'update' authentication of edit/update pages for non-signed-in users should deny access to 'update'
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:130

  3) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'edit'
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:130

  4) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'update'
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:130

  5) UsersController PUT 'update' GET 'edit' should be successful
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
      Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:168

  6) UsersController PUT 'update' GET 'edit' should have a link to change the Gravatar
     Failure/Error: @user = Factory(:user)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/controllers/users_controller_spec.rb:168

It doesn’t seem to like the line:

before(:each) do
      @user = Factory(:user)
      test_sign_in(@user)
end

Factory Code

# By using the symbol ':user', we get Factory Girl to simulate the User model.
Factory.define :user do |user|
  user.name                  "Insert Name"
  user.email                 "email@example.com"
  user.password              "password"
  user.password_confirmation "password"
end

I assume that’s what you want.

User Controller Spec

require 'spec_helper'

describe UsersController do
  render_views

  describe "GET 'show'" do

before(:each) do
  @user = Factory(:user)
end

it "should be successful" do
  get :show, :id => @user
  response.should be_success
end

it "should find the right user" do
  get :show, :id => @user
  assigns(:user).should == @user
end

it "should include the user's name" do
     get :show, :id => @user
     response.should have_selector("h1", :content => @user.name)
end

it "should have a profile image" do
    get :show, :id => @user
    response.should have_selector("h1>img", :class => "gravatar")
end
  end

  describe "GET 'new'" do

it "should be successful" do
  get :new
  response.should be_success
end
  end

  describe "POST 'create'" do

describe "failure" do

  before(:each) do
    @attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
  end

  it "should not create a user" do
    lambda do
      post :create, :user => @attr
    end.should_not change(User, :count)
  end 

  it "should render the 'new' page" do
    post :create, :user => @attr
    response.should render_template('new')
  end
    end

describe "success" do

  before(:each) do
    @attr = { :name => "New User", :email => "user@example.com", :password => "password", :password_confirmation => "password" }
  end 

  it "should create a user" do
    lambda do
      post :create, :user => @attr
    end.should change(User, :count).by(1)
  end 
  it "should have a welcome message" do
          post :create, :user => @attr
          flash[:success].should =~ /Welcome to InTouch/i
  end

  it "should sign the user in" do
    post :create, :user => @attr
    controller.should be_signed_in
  end 
end
  end 

  describe "PUT 'update'" do

before(:each) do
  @user = Factory(:user)
  test_sign_in(@user)
end

describe "failure" do

  before(:each) do
    @attr = { :email => "", :name => "", :password => "", :password_cofirmation => "" }
  end 

  it "should render the 'edit' page" do
    put :update, :id => @user, :user => @attr
    response.should render_template('edit')
  end 

describe "success" do

  before(:each) do
    @attr = { :name => "New Name", :email => "user@example.org", :password => "newpassword", :password_confirmation => "newpassword" }
  end

  it "should change the user's attributes" do
    put :update, :id => @user, :user => @attr
    @user.reload
    @user.name.should == @attr[:name]
    @user.email.should == @attr[:email]
  end 

  it "should redirect to the user show page" do
    put :update, :id => @user, :user => @attr
    response.should redirect_to(user_path(@user))
  end 

  it "should have a flash message" do
    put :update, :id => @user, :user => @attr
    flash[:success].should =~ /updated/
  end 
    end 
  end 

  describe "authentication of edit/update pages" do

before(:each) do
  @user = Factory(:user)
end

describe "for non-signed-in users" do

  it "should deny access to 'edit'" do
    get :edit, :id => @user
    response.should redirect_to(signin_path)
  end

  it "should deny access to 'update'" do
    put :update, :id => @user, :user => {}
    response.should redirect_to(signin_path)
  end
    end

describe "for signed-in users" do

  before(:each) do
    wrong_user = Factory(:user, :email => "user@example.net")
    test_sign_in(wrong_user)
  end

  it "should require matching users for 'edit'" do
    get :edit, :id => @user
    response.should redirect_to(root_path)
  end

  it "should require matching users for 'update'" do
    put :update, :id => @user, :user => {}
    response.should redirect_to(root_path)
  end
    end
  end

  describe "GET 'edit'" do

before(:each) do
  @user = Factory(:user)
  test_sign_in(@user)
end

it "should be successful" do
  get :edit, :if => @user
  response.should be_success
end

it "should have a link to change the Gravatar" do
  get :edit, :id => @user
  gravatar_url = "http://gravatar.com/emails"
  response.should have_selector("a", :href => gravatar_url, :content => "change")
    end
  end
end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:39:16+00:00Added an answer on May 23, 2026 at 2:39 am

    Show us user factory code. I guess that you using non-unique email for each user. Try creating email for user with the help of Factory sequence. Also, check you _test db on duplicate records.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.