[rspec-users] RSpec 2 example group block scope

Ashley Moran ashley.moran at patchspace.co.uk
Mon Jul 26 03:55:10 EDT 2010


Hi again

As part of the refactoring I'm doing, I'm writing out quite a bit of metaprogramming.  The easiest way to prove constant lookup is working is to create a new class in an example group, and use its name.  But the scope of the example group definition appears to be the containing module, which gives the following behaviour:

  module MyModule
    describe "MyClass" do
      class TestClass; end
    
      # This passes
      it "has access to the test class" do
        expect {
          TestClass
        }.to_not raise_error
      end
      
      # This fails
      it "hides the test class somewhere?" do
        TestClass.name.should_not eq "MyModule::TestClass"
      end
    end
  end

  module MyModule
    describe "MyClass doing something else" do
      # This fails
      it "has a different scope to the example above" do
        expect {
          TestClass
        }.to raise_error(NameError)
      end
    end
  end

I've got a few questions:

* Is this intended behaviour, or just merely the way RSpec 2 happens to work?

* Would a more unique scope be useful? (To me it would, obviously... don't know about the tradeoff in general though)

* Has this changed since RSpec 1.3?  (I guess I could install and find out...)

Cheers
Ash

-- 
http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran





More information about the rspec-users mailing list