2

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
  • Thats a tough one. I wouldn't go so far as to fix that in unit tests. Testing for requires is a chicken and egg situation, checking for a require after it happens won't work. Perhaps using Cucumber (idk if it uses Set or not) and Capybara for full browser tests will draw the issue out.
    – stuartc
    Dec 3, 2012 at 11:53

1 Answer 1

3
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.

2
  • Very clever. I'm going to accept this answer because it gets the job done and even though it's less than ideal it appears to be the best we have... at least until Ruby provides a way to unload libraries, or RSpec provides a way to run tests in a separate, unpolluted sandbox. Dec 7, 2012 at 10:12
  • Oops... I accidentally ended my last comment prematurely. Here's the rest: I don't plan on proactively writing tests like this. :-) I just needed a test (no matter how ugly) to pair alongside the bug fix so I could commit the two together, so this is likely a one-time need. This does the trick, and the cleverness makes up for the (albeit necessary) yuckiness. :-) Thanks! Dec 7, 2012 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.