Skip to content

Instantly share code, notes, and snippets.

@Ragmaanir
Created August 9, 2014 12:11
Show Gist options
  • Save Ragmaanir/2d038cf78a84cf37f461 to your computer and use it in GitHub Desktop.
Save Ragmaanir/2d038cf78a84cf37f461 to your computer and use it in GitHub Desktop.
module X
C = 1
end
describe 'Test' do
include X
it '...' do
p C
end
end
@cupakromer
Copy link

RSpec doesn't do anything magical, it follows normal ruby scoping and constant look up. Including a module does not include it's constants in Ruby. It includes only the public instance methods. If you need module constants to be available, the spec either needs to be in the module, or use the fully qualified namespace, i.e. X::C.

module X
  C = 1
  class Foo
  end
end

RSpec.describe "test" do
  it '...' do
    p X::C
  end
end

# If you are unit testing a class in a module this is a common idiom
module X
  RSpec.describe Foo do
    it '...' do
      p C
    end
  end
end

I would not suggest wrapping arbitrary specs inside a module just to access the constant. That idiom really only makes sense when unit testing something inside the module. Then it allows for constant references that mirror the code and the specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment