[rspec-users] How do I extend ExampleGroup in Rspec 2?

David Chelimsky dchelimsky at gmail.com
Sat Aug 7 11:15:27 EDT 2010


On Aug 7, 2010, at 10:04 AM, Kristian Mandrup wrote:

> Thanks, but it didn't work. The following somewhat ugly hack works
> however.
> 
> module RSpec::Core
>  class ExampleGroup
>    def with_generator &block
>      RSpec::Generator.with_generator &block
>    end
> 
>    def setup_generator test_method_name=nil, &block
>      RSpec::Generator.setup_generator test_method_name, &block
>    end
>  end
> end

Please submit an issue for this - it should work as I suggested: http://github.com/rspec/rspec-core/issues

> 
> 
> On Aug 7, 4:09 pm, David Chelimsky <dchelim... at gmail.com> wrote:
>> On Aug 7, 2010, at 8:23 AM, Kristian Mandrup wrote:
>> 
>> 
>> 
>>> I simply want all methods of a module to be always available within
>>> the context of an Example group.
>> 
>>> module RSpec
>>>  module Generator
>>>      def with_generator &block
>>>       ...
>>>      end
>> 
>>>      def setup_generator test_method_name=nil, &block
>>>        ...
>>>      end
>>>  end
>>> end
>> 
>>> How do I achieve this?
>> 
>>> In RSpec 1 I think you would use ExampleGroupFactory
>> 
>>> I thought I could do it something like this with RSpec 2?
>> 
>>> RSpec.configure do |c|
>>> c.extend RSpec::Generator
>>> end
>> 
>>> I want to be able to do something like this
>> 
>>>  before :each do
>> 
>> before hooks are eval'd in the scope of an example, which is an _instance_ of the example group class. Try using include instead of extend:
>> 
>> c.include RSpec::Generator
>> 
>> HTH,
>> David
>> 
>> 
>> 
>>>    setup_generator 'migration_generator' do
>>>      tests MigrationGenerator
>>>    end
>>>  end
>> 
>>>  it "should generate create_user migration" do
>>>    with_generator do |g|
>>>      ...
>>>  end
>> 
>>> Whereas now I have to do it like this, which I find a bit ugly and
>>> cumbersome
>> 
>>>  it "should generate create_user migration" do
>>>    RSpec::Generator.with_generator do |g|
>>>      name = 'create_users'
>>>    end
>>>  end
>> 
>>> Thanks.
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-us... at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>> 
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list