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 9313385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:52:12+00:00 2026-06-19T01:52:12+00:00

I followed The Ruby on Rails Tutorial Book by Michael Hartl , modified it,

  • 0

I followed The Ruby on Rails Tutorial Book by Michael Hartl, modified it, added Devise for user authentication and ran into an issue with the uniqueness tests of the email.

# spec/models/user_spec.rb

require 'spec_helper'

describe User do
  before do
    @user = User.new(username: 'ExampleUser',
                     email:    'user@example.com',
                     password: 'passworD123')
    @user.save
  end

  subject { @user }

  it { should respond_to(:email) }
  it { should respond_to(:name) }
  it { should respond_to(:username) }
  it { should respond_to(:password) }

  it { should be_valid }

  describe 'username is already taken' do
    before do
      user_with_same_username = @user.dup
      user_with_same_username.username = @user.username.upcase
      user_with_same_username.email = 'a@b.c'
      user_with_same_username.save
    end

    it { should_not be_valid }
  end

  describe 'email address is already taken' do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.username = 'differentUsername'
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  .
  .

Failures:

  1) User email address is already taken
     Failure/Error: it { should_not be_valid }
       expected valid? to return false, got true
     # ./spec/models/user_spec.rb:104:in `block (3 levels) in <top (required)>'

The uniqueness of email is set although it is not needed because Devise does it already. I changed user_with_same_email.save to user_with_same_email.save! and then I got a validation error for the email address because it’s already taken:

1) User email address is already taken
   Failure/Error: user_with_same_email.save!
   ActiveRecord::RecordInvalid:
     Validation failed: Email has already been taken
   # ./spec/models/user_spec.rb:101:in `block (3 levels) in <top (required)>'

The return value of user_with_same_email.save is false and it { should_not be_valid } checks this case, but why is the test still failing?

  • 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-19T01:52:13+00:00Added an answer on June 19, 2026 at 1:52 am

    Concerning your actual issue, the reason is simple: you’re not testing upon the proper subject

    Other guidelines:

    • Do not save an object if you know it’s invalid, you’re just wasting time.

    • Same for your first specs, do you really need a persisted object? I guess not..

    • Last but not least: consider using factories, it’s reusable and will let you have more consistent tests.

    Here is what your specs could look like:

    require 'spec_helper'
    
    describe User do
      let(:user_attributes) { {
          username: 'ExampleUser',
          email:    'user@example.com',
          password: 'passworD123'
        }
      }
    
      subject(:user) { User.new(user_attributes) }
    
      it { should respond_to(:email) }
      it { should respond_to(:name) }
      it { should respond_to(:username) }
      it { should respond_to(:password) }
      it { should be_valid }
    
      context 'with existing user in db' do
        before(:each) { user.save }
    
        describe 'username is already taken' do
          subject(:user_with_same_username) { User.new(user_attributes.merge(email: 'another@email.com')) }
          it { should_not be_valid }
        end
    
        describe 'email address is already taken' do
           subject(:user_with_same_email) { User.new(user_attributes.merge(username: 'another name')) }
           it { should_not be_valid }
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to rails and working my way into the tutorial here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book I've
I am new to Rails and finished Michael Hartl's Ruby on Rails 3 Tutorial.
I am following Micheal Hartl's Ruby on rails tutorial 2.3 Edition and I have
I'm trying to learn to use Ruby on Rails (going through Hartl's tutorial for
I followed this tutorial http://pragmaticstudio.com/blog/2010/9/23/install-rails-ruby-mac to install Ruby on Rails on Mac OS X
hey i followed a ruby on rails guide when i tried to run it
I'm new to Ruby on rails and followed the Labs of railsforzombies.org and passed
I followed RailsCasts authentication from scratch (http://railscasts.com/episodes/250-authentication-from-scratch-revised) using the bcrypt-ruby gem and have the
I've followed Rails 3.1 on Heroku Cedar tutorial, as well as another half a
I'm trying to do Exercise 2 of Chapter 8.5 in Michael Hartl's Ruby on

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.