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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:11:44+00:00 2026-06-15T10:11:44+00:00

I have a Ruby program that fails at runtime, but works when I test

  • 0

I have a Ruby program that fails at runtime, but works when I test it with RSpec. I know the cause of the bug and how to fix it (see below), but I can’t figure out how to build a failing RSpec test which proves the existence of the bug.

Imagine the following Ruby:

foobar.rb

class Foobar
  attr_reader :fruit
  def initialize
    @fruit = Set.new ["Apple", "Banana", "Kiwi"]
  end
end

The above code uses a Set, but it fails to “require ‘set'”. This causes it to fail at runtime:

$ irb
> require './foobar.rb'
> f = Foobar.new
NameError: uninitialized constant Foobar::Set

Before fixing the oversight, I wanted to build a simple RSpec test that proves the bug. My test looks like this:

foobar_spec.rb

require 'rspec'
require './foobar.rb'

describe Foobar do
  it "can be initialized" do
    expect { Foobar.new }.to_not raise_error
  end
end

Running the test, I was surprised to see that it passes:

$ rspec foobar_spec.rb
.

Finished in 0.00198 seconds
1 example, 0 failures

After a little digging, I learned that RSpec loads Set for itself. This has the consequence of making Set available to the code it tests, and in my case concealing a bug.

I had the idea of “unloading/unrequiring” Set in my test. The closest I came was this code:

Object.send(:remove_const, :Set)

That indeed causes the test to fail, but unfortunately it also prevents Set from being loaded again by a future ‘require’, meaning it continued to fail even after I added require ‘set’ inside foobar.rb.

Is there a better way to unload gems at runtime? If not, what can I do to make this test fail as it should?

  • 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-15T10:11:44+00:00Added an answer on June 15, 2026 at 10:11 am
    require 'rspec'
    
    describe 'foobar.rb' do
      it "can instantiate Foobar" do
        `ruby -e 'Foobar.new' -r./foobar.rb`
        $?.exitstatus.should == 0
      end
    end
    

    works for the one case you mentioned. That said, I wouldn’t recommend this approach. To cover all the cases where a class is referenced, you’d need to run all your specs this way, since the class reference could appear anywhere in your code.

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

Sidebar

Related Questions

so i have ruby script that simply writes to a test.txt file with hello
I have a Ruby program that zips a directory tree of XML files using
I have a Ruby program that loads up two very large yaml files, so
I have a Ruby program that generates files that contain a Module definition :
I'm writing a ruby program that need raw cpu power (I know ruby is
Does anyone have or know of a Ruby script that converts text to html?
I have a basic ruby program, that listens on a port (53), receives the
I have a Ruby program that takes as input an HTML document. The structure
I have a Ruby program that, amongst other things, executes user-defined blocks of code
This question contains java and ruby. I have a java program that I make

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.