Jarmo Pertman
2010-Jun-16 14:11 UTC
[rspec-users] 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
Matt Wynne
2010-Jun-16 14:20 UTC
[rspec-users] accessing constants from another module within examples
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
David Chelimsky
2010-Jun-16 14:22 UTC
[rspec-users] accessing constants from another module within examples
On Jun 16, 2010, at 9:11 AM, 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?Seems like a bug. Please submit it to http://github.com/rspec/rspec-core/issues for rspec-2, http://rspec.lighthouseapp.com to make sure it gets backported to rspec-1. Thx, David
Jarmo Pertman
2010-Jun-16 14:36 UTC
[rspec-users] accessing constants from another module within examples
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 On Jun 16, 5:20?pm, Matt Wynne <m... at mattwynne.net> wrote:> That wouldn''t work in regular ruby either. You need to run the describe block inside the module / namespace.
Jarmo Pertman
2010-Jun-16 14:46 UTC
[rspec-users] accessing constants from another module within examples
Done! Jarmo On Jun 16, 5:22?pm, David Chelimsky <dchelim... at gmail.com> wrote:> Seems like a bug. Please submit it tohttp://github.com/rspec/rspec-core/issuesfor rspec-2,http://rspec.lighthouseapp.comto make sure it gets backported to rspec-1. > > Thx, > David
Matt Wynne
2010-Jun-16 14:50 UTC
[rspec-users] accessing constants from another module within examples
On 16 Jun 2010, at 15:20, Matt Wynne wrote:> > 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.I stand corrected. It absolutely does work. Sorry for the duff information.> >> >> Best Regards, >> Jarmo >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users