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

The Archive Base Latest Questions

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

I am using some spec_helper.rb methods to generate RSpec examples and one of them

  • 0

I am using some spec_helper.rb methods to generate RSpec examples and one of them is dependent on data from another one.

Example spec_helper.rb:

def it_should_be_big(objects)
  @tested_objects ||= []

  objects.each do |obj|
    it "should be big (#{obj.name})" do
      obj.should be_big
    end
  end

  @tested_objects += objects
end

def it_should_be_small(objects)
  @tested_objects ||= []

  objects.each do |obj|
    it "should be small (#{obj.name})" do
      obj.should be_small
    end
  end

  @tested_objects += objects
end

def it_should_have_tested_for_all_objects
  it "should test for all objects" do
    @tested_objects ||= []

    (all_objects - @tested_objects).should == []

    @tested_objects = []
  end
end

Example something_spec.rb:

describe "something" do
  it_should_be_big(some_objects)
  it_should_be_small(some_other_objects)

  it_should_have_tested_for_all_objects
end

I know the code does not make much sense but it follows the actual code where it matters (the @tested_objects variable).

When I run the specs, it can’t find the @tested_objects variable (I guess it uses another variable space for inside the example blocks). Is there a way to pass the variable to inside the example block of the final helper method?

RSpec 2.5, Rails 3.0.4, Ruby 1.8.7

  • 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:43:39+00:00Added an answer on May 22, 2026 at 1:43 am

    Depending on the situation you may want before or share_examples_for.


    workaround: it seems that only local variables are seen by the it. Then you can try this:

    def it_should_have_tested_for_all_objects
      @tested_objects ||= []
    
      tested_objects = @tested_objects
      it "should test for all objects" do
        (all_objects - tested_objects).should == []
      end
    
      @tested_objects = []
    end
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initially create a table using some data loaded from a php mySQL query.
I'm trying to load some data using sql loader. Here is the top of
I'm using some code to extract from a Google Url the keyword of the
How can I, in Java or using some other programming language, add a new
I have some code: using (OAZISDBDataContext ctx = new OAZISDBDataContext()) { IEnumerable<DBContactDetail> details =
I will be developing some applications using mobile barcode scanners and need to choose
I am using some TFindDialog and TReplace Dialogs in my Application. How can I
I'm using some reflection to set up a ListView made up of custom views.
Is it possible to get the IP address of the device using some code?
This is a bit embarrassing scenario but I need solution. I am using some

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.