Accessing constants from another module within examples

Hello.

I wanted to include module into ExampleGroup to write less code within
examples, but was unable to do so. This seems to be strange, because
in regular Class it works and all methods within that module are also
accessible. See the example:

module MyModule
MyConstant = 1

def my_method
2
end
end

describe “including modules” do
include MyModule

it “works” do
my_method.should == 2
MyModule::MyConstant.should == 1
end

it “doesn’t work” do
MyConstant.should == 1
end
end

Running this simple spec will produce one failure with this message:
1)
NameError in ‘including modules doesn’t work’
uninitialized constant MyConstant

Is this an expected behaviour? Is there some other way to access
constants from another module without specifying module name within
examples?

Best Regards,
Jarmo

On 16 Jun 2010, at 15:11, Jarmo P. wrote:

def my_method
end

Is this an expected behaviour? Is there some other way to access
constants from another module without specifying module name within
examples?

That wouldn’t work in regular ruby either. You need to run the describe
block inside the module / namespace.

On 16 Jun 2010, at 15:20, Matt W. wrote:

module MyModule
it “works” do
1)
NameError in ‘including modules doesn’t work’
uninitialized constant MyConstant

Is this an expected behaviour? Is there some other way to access
constants from another module without specifying module name within
examples?

That wouldn’t work in regular ruby either. You need to run the describe block inside the module / namespace.

I stand corrected. It absolutely does work. Sorry for the duff
information.

Why do you think that it’s not working in regular Ruby either?

Check the following example to see that it does:
module MyModule
MyConstant = 1
def my_method
2
end
end

class MyClass
include MyModule

def initialize
p my_method
p MyModule::MyConstant
p MyConstant
end
end

MyClass.new

Jarmo

Done!

Jarmo

On Jun 16, 2010, at 9:11 AM, Jarmo P. wrote:

def my_method
end

Is this an expected behaviour? Is there some other way to access
constants from another module without specifying module name within
examples?

Seems like a bug. Please submit it to
Issues · rspec/rspec-core · GitHub for rspec-2,
http://rspec.lighthouseapp.com to make sure it gets backported to
rspec-1.

Thx,
David