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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:16:44+00:00 2026-06-04T20:16:44+00:00

I’m trying to keep my specs clean and DRY, but I have some tests

  • 0

I’m trying to keep my specs clean and DRY, but I have some tests for an API that are identical except for which version of the API is being tested. I could repeat the specs simply using something like this:

%w( v1 v2 ).each do |version|
  describe "Query #{version} API" do
    it "responds with JSON"
      # make the call using the version 
    end
  end
end

But I’d like something a bit cleaner, and so I’ve written this method:

module RepetitivelyDescribe
  def repetitively_describe(*args, &example_group_block)
    options = args.extract_options!
    options.delete(:for).each do |item|
      item_args = args.collect(&:dup) + [options.dup]
      item_args[0] << " [#{item}]"

      describe(*item_args) do
        example_group_block.call item
      end
    end
  end
end

RSpec::Core::ExampleGroup.extend RepetitivelyDescribe

And then my test could look more like this:

repetitively_describe "Query API", :for => %( v1 v2 ) do |version|
  it "responds with JSON"
    # make the call using the version 
  end
end

I realise this is a little bit of pedantry, but it’s one less level of indentation, and if I’m going to be making this call a lot, I’d like to have it cleaner.

But of course, it’s not working quite as I’d like. The call to describe within my repetitively_describe doesn’t get logged to the RSpec output (when using the documentation format output), though the examples within do get repeated and use the version block argument as expected. Essentially, that level of context is lost (describe blocks outside and inside of the repetitively_describe block are kept).

There’s more detailed example code in a gist should it be needed. Any clues on why this isn’t quite working right?

  • 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-04T20:16:46+00:00Added an answer on June 4, 2026 at 8:16 pm

    So (apologies if I’m repeating stuff you already know) but every time you call describe/context rspec creates a new class that is a subclass of the current example group class (which eventually is a subclass of RSpec::Core::ExampleGroup) and then uses module_eval to evaluate the block in the context of that class. If I run

    describe "foo" do
      puts "#{self}; #{self.superclass}"
      describe "behaviour 1" do
        puts "#{self}; #{self.superclass}"
        context "with x" do
          puts "#{self}; #{self.superclass}"
        end
      end
    end
    

    then the output is

    #<Class:0x007fb772bfbc70>; RSpec::Core::ExampleGroup
    #<Class:0x007fb772bfb180>; #<Class:0x007fb772bfbc70>
    #<Class:0x007fb772bfa5f0>; #<Class:0x007fb772bfb180>
    

    When you call it rspec create an Example object and appends it to a class instance variable on self (the current example group). rspec also sticks the current example group in the example’s metadata, walking up this tree of example groups is what gives you the full description of the example.

    Your repetitively_describe method calls describe, so at the point that you call example_group_block.call item self is indeed the freshly created example group. When the proc gets evaluated, it of course remembers what the value of self was when it was called so your calls to it are made to the example group that was current when repetitively_describe (easily verifiable by sprinkling some calls to check the value of self throughout your code). Similarly a call to describe adds the example group as a child of the outer example group, not the one created by repetitively_describe.

    What you of course need to do is call example_group_block preserving the correct value of self.

    module RepetitivelyDescribe
      def repetitively_describe(*args, &example_group_block)
        options = args.extract_options!
        options.delete(:for).each do |item|
          item_args = args.collect(&:dup) + [options.dup]
          item_args[0] << " [#{item}]"
    
          describe(*item_args) do
            class_exec(item, &example_group_block)
          end
        end
      end
    end
    

    with this change

    describe('foo') do
      repetitively_describe "Query API", :for => %w( v1 v2 ) do |version|
        it "responds with JSON"
      end
    end.descendant_filtered_examples.collect(&:full_description)
    

    outputs ["foo Query API [v1] responds with JSON", "foo Query API [v2] responds with JSON"] instead of ["foo responds with JSON", "foo responds with JSON"] before the change.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT

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.