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

  • SEARCH
  • Home
  • 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 8189403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:16:01+00:00 2026-06-07T03:16:01+00:00

Edit Using the answers to the question I changed the test to the following

  • 0

Edit

Using the answers to the question I changed the test to the following which tests correctly and passes..

describe "when email is already taken" do
  let(:user_with_same_email) { @user.dup }
  before do
    user_with_same_email.email.upcase!
    user_with_same_email.save
  end

  it { user_with_same_email.should_not be_valid }
end

Note: Not using let(:user_with_same_email) { @user.dup } makes the test fail as it cannot find the variable user_with_same_email if it’s simply duplicated in the before block as in the chosen answer to this question.


I have a User model and a user_spec.rb test file which has various validations against the User models attributes.

Previously I was writing the following at the top of my user_spec.rb file to test the User model:

describe User do

  before do
  @user = User.new(name: "Example User", email: "user@example.com",
                 password: "foobar88", password_confirmation: "foobar88")
end
...

I wanted to move this model creation to FactoryGirl so I created a factories.rb file:

FactoryGirl.define do
  factory :user do
    name "foo"
    email { "#{name}@example.com" }
    password "foobar99"
    password_confirmation "foobar99"
  end
end

I then changed my user_spec.rb:

describe User do

  before do
    @user = FactoryGirl.create(:user)
  end
...

Now every test passes as before except one:

describe "when email is already taken" do
  before do
    user_with_same_email = @user.dup
    user_with_same_email.email = @user.email.upcase
    user_with_same_email.save
  end

  it { should_not be_valid }
end

Now unless `FactoryGirl is skipping my email uniqueness validation I can’t figure out what is going wrong here.

My User model validation code:

class User < ActiveRecord::Base

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i unless const_defined?(:VALID_EMAIL_REGEX)

  has_secure_password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :programs

  before_save { self.email.downcase! }

  validates :name, presence: true, length: { maximum: 50 }
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  • 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-06-07T03:16:03+00:00Added an answer on June 7, 2026 at 3:16 am

    The problem is that when you say it { should_not be_valid }, RSpec checks the subject. in this case the subject is User.new (you have “describe User” at the top so unless you specified something else this is the default).

    You want to check the user_with_same_email for validity instead.

    edit:
    Try this, I think it might work:

    describe "when email is already taken" do
      before do
        @user_with_same_email = @user.dup
        @user_with_same_email.email = @user.email.upcase
        @user_with_same_email.save
      end
    
      it { @user_with_same_email.should_not be_valid }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I am using VS2008, .NET 3.5 I have a DataTable, which is populated
I found this question , which has an answer, but facebook changed the token
I have a text file that I want to edit using Java. It has
EDIT: iam using ajax to load text in my content that is why onload
Given a std record edit form using WPF two way binding to a EF
I'm using Visual Studio 2008. I'm currently working with WPF and I'm using Edit->Format
I am using Komodo Edit , a code editor. When I right click on
I've been using Komodo Edit for a small project in Django. The code completion
I'm going to edit an ImageField using jquery ajax,after searching I found out I
I have a page where the user can edit various content using buttons and

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.