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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:32:38+00:00 2026-05-27T06:32:38+00:00

I wonder if there’s any good way to reuse data between implementation and description

  • 0

I wonder if there’s any good way to reuse data between implementation and description of a spec… More particularly, I’d like to be able do something like the following:

describe "#some_method" do
  let(:arg1) { "Sample String 1" }
  let(:arg2) { "Sample String 2" }

  context "with '#{arg1}', its result" do
    specify { some_method(arg1).should == 1 }
  end

  context "with '#{arg2}', its result" do
    specify { some_method(arg2).should == 2 }
  end
end

Of course, this code won’t work – arg1 and arg2 are not accessible outside of spec bodies.
Is it possible to achieve the similar result without using global variables or external classes?

Update:

I’m interested in the output of the spec. Something like this:

#some_method
  with 'Sample String 1' its result
    should == 1
  with 'Sample String 2' its result
    should == 2
  • 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-27T06:32:39+00:00Added an answer on May 27, 2026 at 6:32 am

    The answer is that you don’t use dynamic descriptions. The RSpec way to do this would be

    describe "#some_method" do
      it "extracts the number correctly" do
        some_method("Sample String 1").should == 1
        some_method("Sample String 2").should == 2
      end
    end
    

    It is no problem to hard-code test data in your specs. If you want more complete output, you can use a custom matcher

    require 'rspec'
    
    class Test
      def some_method(str)
        str[/[0-9]+/].to_i
      end
    end
    
    RSpec::Matchers.define :return_value_for_argument do |result, arg|
      match do |actual|
        actual.call(arg) == result
      end
    
      description do
        "return #{result.inspect} for argument #{arg.inspect}"
      end
    end
    
    describe Test do
      let(:test) { Test.new }
      describe "#some_method" do
        subject { test.method(:some_method) }
    
        it { should return_value_for_argument 1, "str 1" }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if there's any way something like this would be possible for value
I wonder if there's any way to do a while loop in django (I
i wonder if there is a good abstraction layer for XLM like ORM for
I wonder if there is any way to differentiate the function calls (with arrays
I wonder is there any way we can create regions in aspx page as
I wonder if there is any way to debug c++ dll called from C#
I wonder if there is a way to decode a JSON-like string. I got
I wonder whether there is any automatic way of determining (at least roughly) the
I wonder if there's any way i could reach and change the check boxes
I wonder if there is any way to put an background image to the

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.