Kristian Mandrup
2010-Aug-07 13:23 UTC
[rspec-users] 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.
David Chelimsky
2010-Aug-07 14:09 UTC
[rspec-users] How do I extend ExampleGroup in Rspec 2?
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 dobefore 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-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Kristian Mandrup
2010-Aug-07 15:04 UTC
[rspec-users] How do I extend ExampleGroup in Rspec 2?
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, 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
David Chelimsky
2010-Aug-07 15:15 UTC
[rspec-users] How do I extend ExampleGroup in Rspec 2?
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 > endPlease 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
David Chelimsky
2010-Aug-07 15:17 UTC
[rspec-users] How do I extend ExampleGroup in Rspec 2?
On Aug 7, 2010, at 10:15 AM, David Chelimsky wrote:> 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/issuesFYI - 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> >> >> >> 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 >