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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:20:11+00:00 2026-05-22T01:20:11+00:00

I’m in the process of learning Ruby on Rails, so treat me like a

  • 0

I’m in the process of learning Ruby on Rails, so treat me like a total neophyte, because I am.

I’ve got a User model with some associated RSpec tests, and the following test fails:

require 'spec_helper'
describe User do

    it 'should require a password' do
        User.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''}).should_not be_valid
    end

end

The relevant part of the User model looks like this:

class User < ActiveRecord::Base
    ...
    validates :password, :presence => true,
                         :confirmation => true,
                         :length => { :minimum => 6 }
    ...
end

Here’s the catch: if I run User.new(...).valid? from a Rails console using the arguments above, it returns false as expected and shows the correct errors (password is blank).

I was using spork/autotest and I restarted both to no avail, but this test also fails even running it directly with rspec. What am I doing wrong here?

EDIT

I tried a few more things with the test. This fails:

        u = User.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})
        u.should_not be_valid

So does this:

        u = User.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})
        u.valid?
        u.errors.should_not be_empty

This passes, confirming that :password is indeed blank:

        u = User.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})
        u.password.should == ''
  • 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-22T01:20:12+00:00Added an answer on May 22, 2026 at 1:20 am

    So, it’s actually spork that is causing the problem. You can turn caching off, so that it won’t need restarting every time :

    http://ablogaboutcode.com/2011/05/09/spork-testing-tip-caching-classes

    I think this is what happens :

    ruby-1.9.2-p180 :020 > u = User.new
     => #<User id: nil, email: ...
    ruby-1.9.2-p180 :021 > u.errors
     => {} 
    ruby-1.9.2-p180 :022 > u.save
     => false 
    ruby-1.9.2-p180 :023 > u.errors
     => {:email=>["can't be blank", "can't be blank"], ...} 
    

    In short, if you change new to create, it will work 🙂 I think that this happens because the matcher be_valid checks on the model validation errors. There can be a deeper explanation, but i think that if you use create instead of new, it will work.

    EDIT : I have a be_valid_verbose version that might help. Just create a ‘be_valid_verbose.rb’ file in your rspec/custom_matchers folder, and inside it write :

    RSpec::Matchers.define :be_valid_verbose do
      match do |model|
        model.valid?
      end
    
      failure_message_for_should do |model|
        "#{model.class} expected to be valid but had errors:n #{model.errors.full_messages.join("n ")}"
      end
    
      failure_message_for_should_not do |model|
        "#{model.class} expected to have errors, but it did not"
      end
    
      description do
        "be valid"
      end
    end
    

    Now check against be_valid_verbose instead of be_valid. It will hopefully present you with some more information on what is happening in your case.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.