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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:08:34+00:00 2026-06-17T13:08:34+00:00

I have a User model which has methods for a next and previous user:

  • 0

I have a User model which has methods for a next and previous user:

def next_user
  User.where("id > ?", id).order("id ASC").first
end

def prev_user
  User.where("id < ?", id).order("id DESC").first
end

I’m trying to setup a test using RSpec and Factory girl to test this, and am not sure of the approach – i’m only getting started with TDD.

I’m trying this:

describe "previous_next" do
   let(:user) { FactoryGirl.create(:user) }
   let(:next_user) { FactoryGirl.create(:user) }

   it "should know the next user" do
     expect(user.next_user).to eq next_user
   end

   it "should know the previous user" do
     expect(next_user.prev_user).to eq user
   end
end

here is the error

1) User previous_next should know the next user
 Failure/Error: expect(user.next_user).to eq next_user

   expected: #<User id: 7624, name: "Example User", email: "sample9@email.com", created_at: "2013-01-13 04:31:19", updated_at: "2013-01-13 04:31:19", password_digest: "$2a$04$mbzI.yXYd9eSSfXbjChROewwvCHLFQI6qq5IUNzAKo9O...", remember_token: "KMSmiEeOr6f_Sgi8KJffIA", admin: false, plan_id: nil, password_reset_token: nil, password_reset_sent_at: nil, stripe_customer_token: nil>
        got: nil

   (compared using ==)
 # ./spec/models/user_spec.rb:93:in `block (3 levels) in <top (required)>'

 2) User previous_next should know the previous user
    Failure/Error: expect(next_user.prev_user).to eq user

   expected: #<User id: 7626, name: "Example User", email: "sample11@email.com", created_at: "2013-01-13 04:31:19", updated_at: "2013-01-13 04:31:19", password_digest: "$2a$04$uzrKmsXmVY7jUdRtfnfhUe89Jgy1176zE2UxdH90imJR...", remember_token: "gl10QvxjSMZYQS3JIgbREg", admin: false, plan_id: nil, password_reset_token: nil, password_reset_sent_at: nil, stripe_customer_token: nil>
        got: nil

   (compared using ==)
 # ./spec/models/user_spec.rb:97:in `block (3 levels) in <top (required)>'

If i’m creating the next_user record as an instance of the :user Factory, why is it evaulating to nil?

  • 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-17T13:08:35+00:00Added an answer on June 17, 2026 at 1:08 pm

    The problem is that let lazily evaluates, it doesn’t create the records until you ask for them.

    Use let! instead of let. That eagerly evaluates them and forces the records to be created before the test.

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

Sidebar

Related Questions

Ive got the following problem. I have a model called user which has a
I have a model 'User', it's a restful resource, and has the default methods
I have a simple User model which is associated to many Town objects using
I have a user model on which I check to make sure that the
I have 2 models: User and Book and a join model ownership which connect
I have a sort of plug-in model in which various complex user controls are
i have a class User which extends Ebean Model. And i defined a dbfile
Here's a scenario: I have a java front end (RCP/SWT) app which currently has
i am working on a simple web app which has a user model and
I have a model which has a collection of users. I'm looping through 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.