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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:31:04+00:00 2026-05-26T06:31:04+00:00

I just wrote a test for testing if a new user creation also consists

  • 0

I just wrote a test for testing if a new user creation also consists of an admin setting. Here is the test:

describe User do

  before(:each) do
    @attr = { 
      :name => "Example User", 
      :email => "user@example.com",
      :admin => "f"
    }
  end

  it "should create a new instance given valid attributes" do
    User.create!(@attr)
  end

  it "should require a name" do
    no_name_user = User.new(@attr.merge(:name => ""))
    no_name_user.should_not be_valid
  end

  it "should require an email" do
    no_email_user = User.new(@attr.merge(:email => ""))
    no_email_user.should_not be_valid
  end

  it "should require an admin setting" do
    no_admin_user = User.new(@attr.merge(:admin => ""))
    no_admin_user.should_not be_valid
  end

end

Then, in my User model I have:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :admin

  has_many :ownerships
  has_many :projects, :through => :ownerships

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true,
                   :length => { :maximum => 50 }

  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }

  validates :admin, :presence => true

end

I clearly created a new user with an admin setting, so why is it saying it’s false? I created the migration for the admin setting as admin:boolean. Did I do something wrong?

Here’s the error:

Failures:

  1) User should create a new instance given valid attributes
     Failure/Error: User.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: Admin can't be blank
     # ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'

Oddly enough, when I comment out validates :admin, :presence => true, the test creates the user correctly but fails on “User should require an admin setting”

EDIT: When I change the @attr :admin value to “t” it works! Why doesn’t it work when the value is 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-05-26T06:31:05+00:00Added an answer on May 26, 2026 at 6:31 am

    From the rails guides:

    Since false.blank? is true, if you want to validate the presence of a
    boolean field you should use validates :field_name, :inclusion => {
    :in => [true, false] }.

    Basically, it looks like ActiveRecord is converting your “f” to false before the validation, and then it runs false.blank? and returns true (meaning that the field is NOT present), causing the validation to fail. So, to fix it in your case, change your validation:

    validates :admin, :inclusion => { :in => [true, false] }
    

    Seems a little hacky to me… hopefully the Rails developers will reconsider this in a future release.

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

Sidebar

Related Questions

I just wrote some code to test the behavior of std::equal, and came away
I just lost 50% of my answer on a test because I wrote the
I'm just beginning to learn python. I wrote an example script to test OOP
I just wrote a new web part and now I am getting this error
I just wrote some new utility methods within a non-page class for an existing
I'm fairly new to the unit testing world, and I just decided to add
I am new to unit testing, just getting into it using Check for C.
I am new to unit testing and I am trying to test some of
I just wrote this test code in my CustomUIPanel class: public static void main(String[]
I just wrote my first web service so lets make the assumption that my

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.