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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:17:53+00:00 2026-05-26T09:17:53+00:00

If I have rspec test like following: context main context do before(:all) do #

  • 0

If I have rspec test like following:

context "main context" do

  before(:all) do
     # code for before :all
     puts "my fore :all"
  end 
  describe "Scenario-1" do
     context "my context 1" do
        it "should blablabla" do

     end
        it "should blablabla" do

     end
      end
      context "my context 2" do
        it "should blablabla" do

    end
        it "should blablabla" do

     end
      end
    end # end of describe "Scenario-1"

    describe "Scenario-2"
       context "my context 3" do
         it "should blablabla" do

     end
         it "should blablabla" do

     end
       end
    end #end of "Scenario-2"

end #end of main context

Two questions to ask:

1. Is it so that the before(:all) declare actually get called in each sub context ? I thought it is only called once during the whole test, but when I run my test, What I experienced is that the code in before(:all) get executed in each context, that’s it get run when each context started, why?

(As you noticed I have “puts” as part of my before(:all) code, and I saw this puts in each sub-context when run the test, why? isn’t before(:all) should only be executed once during the whole test??)

2. When I run my test, Why the order of the test running is from bottom context to up context (while inside each context, the order of “it” is up-down)? How to change the test order on context level then?

  • 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-26T09:17:54+00:00Added an answer on May 26, 2026 at 9:17 am

    1) before(:all) should be running only once… but there is a known isue about it doing exactly what you’ve pointed out. Discussed here:
    http://rubyforge.org/pipermail/rspec-users/2010-September/018

    2) tests must run independently of one another. to ensure that you aren’t making any assumptions. Thus testing suites often run the tests in random or reverse order – to be sure that you aren’t doing that.

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

Sidebar

Related Questions

I have the following RSpec example which is passing: describe UsersController do it should
I have following code like this in 'artists_helpers' class. def round_to(x) (self * 10**x).round.to_f
I have problems to get rspec running properly to test validates_inclusion_of my migration looks
I am writing rspec test for my Cars class, and have a question regarding
I've got an rspec test which looks something like this: require 'require_all' require_rel '../spec_helper'
RSpec newbie here. I'm trying to test my models, which have methods that uses
I'm adding more rspec testing to my app and would like to test a
So I'm using rspec to test my code as I'm going through the Rails
I would like to test the following helper module function: module UploadsHelper def custom_img_tag(upload,
I have a suite of RSpec tests I want to group under the following

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.