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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:06:25+00:00 2026-06-09T00:06:25+00:00

I have a basic doubt. If the rspec file contains many contexts: describe Name

  • 0

I have a basic doubt.
If the rspec file contains many contexts:

describe Name do
context "number1" do
.......
.......
end
context "number 2" do
.......
.......
end
context "number 3" do
.......
.......
end

How should the functions from each of the contexts be described in the .rb file? Should they be in the same class or different class? Is there any book I can read to improve my knowledge about this?

  • 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-09T00:06:27+00:00Added an answer on June 9, 2026 at 12:06 am

    The structure I use when defining rspec files (based on reading I’ve done on rspec) is that you use describes to describe specific functions, and context to talk about a specific context of state and/or path through the function.

    Example class:

    class MyClass
        def self.my_class_method(bool)
            if bool == true
                return "Yes"
            else
                return "No"
            end
        end
    
        def my_instance_method
            today = Date.today
            if today.month == 2 and today.day == 14
                puts "Valentine's Day"
            else
                puts "Other"
            end
         end
    end
    

    As you can see, I’ve defined a class method and an instance method that do really silly and random functions. But the point is this: the class method will do something different based on the argument, and the instance method will do something different based on some outside factor: you need to test all these, and these are different contexts. But we will describe the functions in the rspec file.

    Rspec file:

    describe MyClass do
        describe ".my_class_method" do
            context "with a 'true' argument" do
                it "returns 'Yes'." do
                    MyClass.my_class_method(true).should eq "Yes"
                end
            end
    
            context "with a 'false' argument" do
                it "returns 'No'." do
                    MyClass.my_class_method(false).should eq "No"
                end
            end
        end
    
        describe "#my_instance_method" do
            context "on Feb 14" do
                it "returns 'Valentine's Day'." do
                    Date.stub(:today) { Date.new(2012,2,14) }
                    MyClass.new.my_instance_method.should eq "Valentine's Day"
                end
            end
    
            context "on a day that isn't Feb 14" do
                it "returns 'Other'." do
                    Date.stub(:today) { Date.new(2012,2,15) }
                    MyClass.new.my_instance_method.should eq "Other"
                end
            end
        end
    end
    

    So you can see the describe is for saying what method you’re describing, and matches up with the name of a method in your class. The context is used to evaluate different conditions the method can be called in, or different states that affect the way the method works.

    Hope this helps!

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

Sidebar

Related Questions

I have a basic doubt. I have come across many open source OAuth libs.But
I have a basic doubt that, How can we have both CLR's on a
I have a very basic doubt regarding the method that gets executed when app
I have a pretty basic doubt on dirty read/writes to a value stored in
Well, I think I have a very basic doubt here: I'm developing an app
I have basic doubt regarding executable stored in ROM. As I know the executable
I have a basic doubt. Internally how are events represented as methods or as
I have one basic doubt. I have a process which uses a shared library.
I have a very basic doubt regarding cloud computing that is catching up pretty
I have a very basic doubt here. I have two very simple C codes

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.