[rspec-users] accessing constants from another module within examples

Matt Wynne matt at mattwynne.net
Wed Jun 16 10:20:22 EDT 2010


On 16 Jun 2010, at 15:11, Jarmo Pertman wrote:

> 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?

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

> 
> Best Regards,
> Jarmo
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list