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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:12:05+00:00 2026-05-26T03:12:05+00:00

I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would

  • 0

I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would like to refactor the following sample code in a my spec file:

describe "Making things" do
  it "should make a thing" do
    # Make the thing
    ...

    # This is the same statement as that present in the "should make another
    # thing" example (read below for more information)
    response.body.should include("Hello World")
  end

  it "should make another thing" do
    # Make another thing
    ...

    # The same statement as that present in the "should make a thing" example
    response.body.should include("Hello World")
  end
end

How can I refactor the above response.body.should include("Hello World") code so to write less code? That is, how can I test the response.body content by using one statement valid for both spec examples?

  • 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-26T03:12:05+00:00Added an answer on May 26, 2026 at 3:12 am

    Use shared_examples_for.

    Like this:

    describe "Making things" do
      before do
        @user.new
      end
    
      shared_examples_for "normal case" do
        it "shows hello world" do
          response.body.should include("Hello World")
        end
    
        # more tests could be here
    
      end
    
      context "making a thing" do
        before(:each) do
          # make thing
        end
        it_should_behave_like_a "normal case"          
      end
    
      context "making another thing" do
        before(:each) do
          # make another thing
        end
        it_should_behave_like_a "normal case"          
      end
    end
    

    See documentation here.

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

Sidebar

Related Questions

I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I would like
I'm following the ruby on rails tutorial: http://railstutorial.org/chapters/static-pages#top I'm up to using rspec. Having
I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I am
I'm using rspec with ruby on rails for testing. Question - In a spec
I am using Ruby on Rails with Cucumber and Capybara. How would I go
How can I start using Rspec with an existing Ruby on Rails 2.3.5 project
I'm using: Rails 3.0.7 and Rspec 2.5.0 via rvm When I run this spec
I am using cucumber and RSpec for my new ruby on rails application and
I'm using rr (the mocking framework) and rspec with ruby-on-rails. Also, I'm using 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.