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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:46:13+00:00 2026-05-15T23:46:13+00:00

I have the following test, with two almost identical blocks. Now i am looking

  • 0

I have the following test, with two almost identical blocks. Now i am looking for ways to refactor this cleanly.

The test:

context "with the d1 configuration" do
  before (:each) do
    # send a message 
    @envelope = Factory(:envelope, :destination => '32495xxxxxx', :message => 'Message sent by d1')
    @distributor = Distributor.find_by_name(Distributor::D1)
    @result = @envelope.send_to(@distributor)
  end
  it "should created a new sms-message" do
    @envelope.sent_messages.size.should == 1
  end

  it "should have created one sms-message linked to the envelope and distributor" do
    sms = @envelope.sent_messages.find_by_distributor_id(@distributor.id)
    sms.should be_instance_of(SentMessage)
    sms.external_message_id.should_not == nil
    sms.sent_message_status_id.should == SentMessageStatus::IN_PROGRESS
  end

  it "should add a logline for the creation of the sms-message" do
    @envelope.log_lines.size.should == 2
    @envelope.log_lines.last.message.should =~ /^Sent message/
  end
end


context "with the correct d2 configuration" do
  before (:each) do
    # send a message 
    @envelope    = Factory(:envelope, :destination => '32495xxxxxx', :message => 'Message sent by d2')
    @distributor = Distributor.find_by_name(Distributor::D2)
    @result = @envelope.send_to(@distributor)
  end
  it "should created a new sms-message" do
    @envelope.sent_messages.size.should == 1
  end

  it "should have created one sms-message linked to the envelope and distributor" do
    sms = @envelope.sent_messages.find_by_distributor_id(@distributor.id)
    sms.should be_instance_of(SentMessage)
    sms.external_message_id.should_not == nil
    sms.sent_message_status_id.should == SentMessageStatus::IN_PROGRESS
  end

  it "should add a logline for the creation of the sms-message" do
    @envelope.log_lines.size.should == 2
    @envelope.log_lines.last.message.should =~ /^Sent message/
  end
end

As you can tell, two identical code blocks, each for a different distributor, D1 and D2 (in our project they have more meaningful names :)) — and now i need to add a third distributor. How do i go about this?

I can loop over an array containing the changing parts (in this case: distributor-name and the message contents). But can i also change the test-name?

What are the best approaches here? Is it possible to make some kind of test-template, where you can fill in certain values and execute that?

  • 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-15T23:46:14+00:00Added an answer on May 15, 2026 at 11:46 pm

    Yes, you can loop through an array/hash full of examples and yes you can rename contexts based on that but you’ll have to be aware of scoping issues – eg a context is a class-level scope, but a test is an instance.
    Thus you have to setup these things in instance-variables in the “setup” section of a context.
    I’ve mainly done this stuff with unit:test+shoulda (rather than rspec) so I may have messed up the scoping rules somewhat, but they should be similarish

    Note: I haven’t tested the code below, so it may be prey to such issues…

    # name this better than I have    
    CONFIGS = {'d1' => {:name => Distributor::D1
                        :destination => '32495xxxxxx',
                        :message => 'd1 message'}, 
               'd2' => {:name => Distributor::D2
                        :destination => '98765xxxxxx',
                        :message => 'd2 message'}
               } # etc
    
    CONFIGS.each do |display_name, dist_hash|
      context "with the #{display_name} configuration" do
        before (:each) do
          # scope the value-hash here to make it available to test-cases 
          # (you don't have to if you're just using it in the setup section)
          @dist_hash = dist_hash
          # send a message 
          @envelope = Factory(:envelope, :destination => @dist_hash[:destination], :message => @dist_hash[:message])
          @distributor = Distributor.find_by_name(@dist_hash[:name])
          @result = @envelope.send_to(@distributor)
        end
        it "should created a new sms-message" do
          @envelope.sent_messages.size.should == 1
        end
    
        it "should have created one sms-message linked to the envelope and distributor" do
          sms = @envelope.sent_messages.find_by_distributor_id(@distributor.id)
          sms.should be_instance_of(SentMessage)
          sms.external_message_id.should_not == nil
          sms.sent_message_status_id.should == SentMessageStatus::IN_PROGRESS
        end
    
        it "should add a logline for the creation of the sms-message" do
          @envelope.log_lines.size.should == 2
          @envelope.log_lines.last.message.should =~ /^Sent message/
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following spec to test controller method: context #create do it should redirect
This is a scjp mock exam question. Suppose I have the following two files:
I have the following set up .test div:first-child {}; <div class=test id=one> <div id=two>
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I've got the following problem (test version available http://jsfiddle.net/qmP8R/2/ ): I have two <div>
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have following databases: test table1 fields: id, password, name, lastname test2 table2 fields:
I have following string: Test, User < test@test.com >, Another, Test < another@test.com >,
I have the following test sample: <Window x:Class=WpfScrollTest.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=200 Width=200> <Border>
I have the following test code use Data::Dumper; my $hash = { foo =>

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.