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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:58:07+00:00 2026-06-16T09:58:07+00:00

I just added the Rolify gem, and am running some rspec tests. 2 tests

  • 0

I just added the Rolify gem, and am running some rspec tests.

2 tests are as follows:

  describe "roles" do

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

    it "should not approve incorrect roles" do
      @user.add_role :moderator
      @user.has_role? :admin
      should be_false
    end

    it "should approve correct roles" do
      @user.add_role :moderator
      @user.has_role? :moderator
      should be_true
    end

  end

The test result is:

  1) User roles should not approve incorrect roles
     Failure/Error: should be_false
       expected: false value
            got: #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, name: nil, position: nil, location: nil, admin: false, archived: false, public_email: false, created_at: nil, updated_at: nil>
     # ./spec/models/user_spec.rb:70:in `block (3 levels) in <top (required)>'

Finished in 1.37 seconds
7 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:67 # User roles should not approve incorrect roles

Randomized with seed 13074

factories.rb

FactoryGirl.define do

  factory :user do
    sequence(:name)       {|n| "Example User #{n}"}
    sequence(:email)      {|n| "email#{n}@program.com" }
    position              "Regular"
    location              "Anywhere, USA"
    public_email          false
    password              "foobar"
    password_confirmation "foobar"
    confirmed_at          Time.now
  end
end

How is the first test is failing with a nil object, but the second is passing?

EDIT

Upon further inspection, any test for should be_true passes, and any test for should be_false fails, regardless of whether the added role matches the checked role.

  • 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-16T09:58:08+00:00Added an answer on June 16, 2026 at 9:58 am

    When your tests do should be_true what is happening is the should call is being delegated to the subject object (see RSpec docs for implicit receiver). In this case, your subject object is a User instance that has not yet been saved to the database. If your user_spec.rb file starts with describe User do, RSpec is automatically providing this default subject of User.new (see RSpec docs for implicit subject).

    What this means is that your tests are essentially doing User.new.should be_true and User.new.should be_false. Since a User object will always evaluate to true, the should be_true test will always pass (although probably not for the reason you wanted it to) and the should be_false will always fail.

    Based on the way your tests are written, maybe you meant something more like this:

    describe "roles" do
    
      before(:each) do
        @user = FactoryGirl.create(:user)
      end
    
      it "should not approve incorrect roles" do
        @user.add_role :moderator
        @user.has_role?(:admin).should be_false
      end
    
      it "should approve correct roles" do
        @user.add_role :moderator
        @user.has_role?(:moderator).should be_true
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just added some Python unit tests to an existing Maven POM but I
I just added an ExecutionHandler to my server pipeline just before my main business
I just added some stylings to the search box which make the corners round
I just added one in-app purchase to my app. When a user click on
I just added validations for a carrierwave image to a model and now tests
I just added some code that was taken from the Apple docs which shows
I just added a local project to sourceforge. I moved some things around, and
I just added a table which holds a url and description for each item
I've just added and configured the devise gem. It's working great except for blocking
I've just added some search functionality to a project of mine which is working

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.