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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:48:59+00:00 2026-06-17T10:48:59+00:00

This is one of those cases where my code is working but my test

  • 0

This is one of those cases where my code is working but my test is failing and I need to know what I am doing wrong?

I have a Project class with an all method that just spits out instances of this class:

class Project

    @@all_projects = []

    def initialize(options)
      @@all_projects << self
    end

    def self.all
    @@all_projects
  end
end

Now Project.all works just fine but the spec I am writing doesn’t.

context "manipulating projects" do
    before do
        options1 = {
            name: 'Building house'
        }

        options2 = {
            name: 'Getting a loan from the Bank'
        }

        @project1 = Project.new(options1)
        @project2 = Project.new(options2)
    end
        it "can print all projects" do
      Project.all.should eq([@project1, @project2])
    end

The failure message I get is:

Project manipulating projects can print all projects
     Failure/Error: Project.all.should eq([@project1, @project2])

       expected: [Building house, Getting a loan from the Bank]
            got: [Building house, Building house, Building house, Getting a loan from the Bank, Building house, Getting a loan from the Bank]

Here is the full spec in a gist: https://gist.github.com/4535863

What am I doing wrong? How can I fix it?

  • 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-17T10:49:00+00:00Added an answer on June 17, 2026 at 10:49 am

    It is doubling the results because it runs the before block for each test, where the class attribute is modified (when two new projects are initialized), and (according to the gist) the test you’re referring to is the second one.

    To avoid the problem you’ll need to reset @@all_projects in an after block:

    after do
      Project.class_variable_set :@@all_projects, []
    end
    

    See also: How can I clear class variables between rspec tests in ruby

    (Thanks to @iain for the suggestion to move the reset code to an after block rather than a before block.)

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

Sidebar

Related Questions

This is one of those I probably should know this, but I don't questions.
I think this is going to be one of those simple-when-you-see-it problems, but it
I have this code here : <select id=test> <option value=1>test1</option> <option value=2 selected=selected>test2</option> <option
This is one of those things that you read once, say aha! and then
Hey, so this is one of those questions that seems obvious, and I'm probably
This is just one of those little things I did all the time in
This is yet one more of those how to switch from running with a
Hey this is going to be one of those dumb questions. I am trying
Good-morning one and all! This is going to end up being one of those
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>

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.