How do I extend ExampleGroup in Rspec 2?

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

On Aug 7, 2010, at 8:23 AM, Kristian M. wrote:

   ...

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

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

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

 RSpec::Generator.setup_generator test_method_name, &block

end
end
end

Please submit an issue for this - it should work as I suggested:

On Aug 7, 2010, at 10:15 AM, David C. wrote:

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: Issues · rspec/rspec-core · GitHub

FYI - this passes for me:

module Foo
def bar
yield
end
end

RSpec.configure do |c|
c.include Foo
end

describe :a do
it “foo” do
yielded = false
bar do
yielded = true
end
yielded.should be_true
end
end