[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rspec Spec::Example::Configuration question

Mark Ryall

3/17/2008 6:05:00 AM

[Note: parts of this message were removed to make it a legal post.]

Can anyone point me in the direction of the correct way to use rspec's
configuration class to mixin modules into example groups (and add
before/after blocks) according to type.

http://rspec.info/rdoc/classes/Spec/Example/Configur...

I expected the following (very silly and contrived) code to prepend_before,
prepend_after and include their respective blocks/mixin _only_ for the specs
with :type => :awe_inspiring but find that they get applied to all example
groups.

require 'spec'

module AMixin
def wow
end
end

Spec::Runner.configure do |config|
config.include(AMixin, :type => :awe_inspiring)
config.prepend_before(:type => :awe_inspiring) do
puts 'before awe_inspiring test'
end
config.prepend_after(:type => :awe_inspiring) do
puts 'after awe_inspiring test'
end
end

describe 'without enthusiasm', :type => :mediocre do
it "should do something without enthusiasm" do
puts 'have wow' if self.respond_to?(:wow)
end
end

describe 'with enthusiasm', :type => :awe_inspiring do
it "should do something with enthusiasm" do
puts 'have wow' if self.respond_to?(:wow)
end
end

1 Answer

David Chelimsky

3/25/2008 3:22:00 PM

0

On Mon, Mar 17, 2008 at 1:04 AM, Mark Ryall <mark.ryall@gmail.com> wrote:
> Can anyone point me in the direction of the correct way to use rspec's
> configuration class to mixin modules into example groups (and add
> before/after blocks) according to type.
>
> http://rspec.info/rdoc/classes/Spec/Example/Configur...
>
> I expected the following (very silly and contrived) code to prepend_before,
> prepend_after and include their respective blocks/mixin _only_ for the specs
> with :type => :awe_inspiring but find that they get applied to all example
> groups.

:type => :whatever is *not* an arbitrary tagging system. It supports
creating your own custom types of ExampleGroups. If it can't find one
registered with the key, it gives you the default.

> require 'spec'
>
> module AMixin
> def wow
> end
> end
>
> Spec::Runner.configure do |config|
> config.include(AMixin, :type => :awe_inspiring)
> config.prepend_before(:type => :awe_inspiring) do
> puts 'before awe_inspiring test'
> end
> config.prepend_after(:type => :awe_inspiring) do
> puts 'after awe_inspiring test'
> end
> end
>
> describe 'without enthusiasm', :type => :mediocre do
> it "should do something without enthusiasm" do
> puts 'have wow' if self.respond_to?(:wow)
> end
> end
>
> describe 'with enthusiasm', :type => :awe_inspiring do
> it "should do something with enthusiasm" do
> puts 'have wow' if self.respond_to?(:wow)
> end
> end

You should be able to do something like this:

class AweInspiring < Spec::Example::ExampleGroup
def wow;end
Spec::Example::ExampleGroupFactory.register(:awe_inspiring, self)
end

Now you could use describe 'with enthusiasm', :type => :awe_inspiring
do and not have to worry about setting up the config to mix modules
in.

HTH,
David