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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:14+00:00 2026-06-10T14:38:14+00:00

I am using some of these tools for the first time. I have read

  • 0

I am using some of these tools for the first time. I have read through the docs but wanted to ask here exactly what I’m trying to achieve.

I have a set of users that I want to test some actions I can do in a controller spec. When each user is created, there are a set of callbacks that take place to create associated objects.

I’d like to have access to these user instances and the associated objects of that ActiveRecord class. So for example, a user will have a set of lists so I’d like to be able to call user1.lists for example.

Also, I’d like to isolate this setup at the top and use either let’s or a before black. It seems that just calling let like this:

# will test that get_count_for_list will return 5
describe ApiController do

  # why same name - seems really confusing!
  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user2) }

doesn’t call the associated callbacks. Is this correct? Or is it possibly a timing issue?

I like the syntax of using let and being able to access these objects in my ExampleGroups such as user.id but can’t access user.lists. Currently I am doing something like:

# will test that get_count_for_list will return 5

describe ApiController do

  # why same name - seems really confusing!
  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user2) }
  let(:user3) { FactoryGirl.create(:user3) }
  
  before do
    FactoryGirl.create(:user2)
    FactoryGirl.create(:user3)
  end

but feel that there has to be a better way. Am I creating these user’s twice?

thx

edit 1

I’ve isolated the code in question here. The global_id value is created via a callback. It exists correctly in the db and can be accessed via the corresponding find_by_email’s but using the user2 var’s doesn’t provide access.

require 'spec_helper'

# will test that get_count_for_list will return 5
describe ApiController do
  # why same name - seems really confusing!
  let!(:user) { FactoryGirl.create(:user) }
  let!(:user2) { FactoryGirl.create(:user2) }
  let!(:user3) { FactoryGirl.create(:user3) }


  
  before do
    session[:user_id]=user.id # works
  end

  describe 'FOLLOW / UNFOLLOW options' do
    it 'shall test the ability to follow another user' do
      puts "user1: " + user.global_id.to_s # doesn't output anything
      u2=User.find_by_email('jo@jo.com') # corresponds to user2
      post :follow, :global_id => user2.global_id # doesn't work
      #post :follow, :global_id => u2.global_id  #works


      u3=User.find_by_email('su@su.com')
      puts "user_3" + u3.global_id.to_s # outputs correct value

      post :follow, :global_id => user3.global_id  #doesn't work
      #post :follow, :global_id => u3.global_id # works


      post :unfollow, :global_id => user.following.sample(1)
      response.code.should eq('200')
    end
  end
end
  • 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-10T14:38:16+00:00Added an answer on June 10, 2026 at 2:38 pm

    Check the rspec doc: https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/helper-methods/let-and-let

    Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked. You can use let! to force the method’s invocation before each example.

    In other words if you use let along with factory_girl a record will not be created before let-variable invocation.

    The correct code is:

    # will test that get_count_for_list will return 5
    describe ApiController do
    
      # why same name - seems really confusing!
      let!(:user) { FactoryGirl.create(:user) }
      let!(:user2) { FactoryGirl.create(:user2) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Eclipse in Ubuntu to edit PHP files. But, unfortunately, some of these
I am using the jQuery cycle plugin to cycle through some images. These images
I have been using node and redis for some time. The problem I am
First some background: I have a website using ASP.NET web forms + jQuery 1.6.4
I'm using some semi-transparent PNGs as background-images on various websites. These are usually something
I'm using AFNetworking for asynchronous calls to a web service. Some of these calls
This is my first time using this site so sorry for any bad formatting
I spent some time trying to generate certain tab icons using Android Asset Studio
I am learning about tools like Chef/Puppet/etc for the first time and was wondering
Here's some sample code that I want to control using jQuery (white button bg

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.