From: Intransition Date: 2009-12-30T01:37:48+09:00 Subject: Re: Using #include at the instance level? On Dec 29, 9:37 am, Robert Klemme wrote: > 2009/12/29 Intransition : > I see to be a bit slow today: how do you want to use modules there? > Do you want to include them in the test code?  What about: > > class TestCase >   def new(cl, &bl) >      @cl = cl >      instance_eval(&bl) >   end > >   alias include extend > end > > TestCase.new String do >   extend Foo >   include Bar > end No no. You got it exactly. Problem is the module's constants are not coming through the #extend (which is effectively the same as the include in the singleton class). Let say I have a library: module MyApp module SomeSpace class FooClass ... end end end In the test cases, instead of having to spell out MyApp::SomeSpace::FooClass everywhere it is needed, it would be nice to include MyApp::SomeSpace, and then just reference FooClass in the tests. > > Instead of making an instance of TestCase for each case, at this point > > it looks like I'll have to create a new subclass of it. > Sorry, you lost me here. Instead of the code you presented I'd have to do something like: class TestCase alias :_new, :new def self.new(&block) Class.new(self, &block)._new end end Haven't tested it yet, but that should allow #include to work no problem. Unfortunately it means defining all my dsl methods at the class level --not even sure the instance level would be of any use in this case either, in which case the ._new can be dropped -- kind of stupid, just to get #include to work. But what else can I do? I almost feel like I'm having a mental block and there is actually an easy way to do this.