Forum: RSpec Options Hash explanation ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B1789b17a54c1731ce9fe4a6480d24e4?d=identicon&s=25 mortench (Guest)
on 2009-05-12 20:57
(Received via mailing list)
The new rspec beta book and the rspec site mentions that it is
possible to add a hash to "describe" and "it" constructs but very,
very litle information about the particulars.

What can the options hash be used for and what should it be used for?

F.x. I have some identical examples that I need to run against a wide
range of different models that have two configuration parameters that
sometimes differ. I am thus thinking of using the has like this in
example groups (tptions which are recoginzed in helpers, macros etc):

describe XXXX-MODEL, :fixturesRelativeRootDir =>
‘testfixtures’, :serviceDescriptorRelativePath => ‘services/
RubyHelloExample.service’

Does usage of custom options like this make sense rspec-wise ?

Finally, I did find out myself that I can get the hash from a helper’s
Spec::Runner.configure.config.before(:all) using “self.class”.
However, this does not work inside nested “it” constructs. Any advice ?
942a74b1b71ca62234ae690b44699ebc?d=identicon&s=25 Randy Harmon (Guest)
on 2009-05-13 01:17
(Received via mailing list)
mortench wrote:
> The new rspec beta book and the rspec site mentions that it is
> possible to add a hash to "describe" and "it" constructs but very,
> very litle information about the particulars.
>
> What can the options hash be used for and what should it be used for?
>
>
This might be used for tagging examples, marking examples for use with
formatters, or other purposes probably not bearing on spec execution.
I'm not saying they could or couldn't be used the way you're suggesting,
but there are other possibilities you should try first IMO.

> F.x. I have some identical examples that I need to run against a wide
> range of different models that have two configuration parameters that
> sometimes differ. I am thus thinking of using the has like this in
> example groups (tptions which are recoginzed in helpers, macros etc):
>
>
I have used a callback for this, but I understand that rspec 1.2 has
macros that can take args the way your shared example can't.   I haven't
dug into them.


Here's the callback approach demonstrated.

describe "shared", :shared => true
  it "..." do
    options = shared_options()
    ...
  end
end

describe "thingy"
  it_should_behave_like "shared"
  def shared_options
    { :foo => 'bar', :baz => 'buz }
  end
end

Randy
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-13 07:29
(Received via mailing list)
On Tue, May 12, 2009 at 12:14 PM, mortench <mortench@gmail.com> wrote:
>
> describe XXXX-MODEL, :fixturesRelativeRootDir =>
> ‘testfixtures’, :serviceDescriptorRelativePath => ‘services/
> RubyHelloExample.service’
>
> Does usage of custom options like this make sense rspec-wise ?
>
> Finally, I did find out myself that I can get the hash from a helper’s
> Spec::Runner.configure.config.before(:all) using “self.class”.
> However, this does not work inside nested “it” constructs. Any advice ?

You can access the group and example options from within an example:

describe "group options hash", :defined_in => :group do
  it "is available in examples via self.class.options" do
    self.class.options[:defined_in].should == :group
  end
end

describe "example options hash" do
  it "is available in examples directly", :defined_in => :example do
    options[:defined_in].should == :example
  end
end

HTH,
David
B1789b17a54c1731ce9fe4a6480d24e4?d=identicon&s=25 mortench (Guest)
on 2009-05-13 22:10
(Received via mailing list)
On 13 Maj, 07:28, David Chelimsky <dchelim...@gmail.com> wrote:
> You can access the group and example options from within an example:
>
> describe "group options hash", :defined_in => :group do
>   it "is available in examples via self.class.options" do
>     self.class.options[:defined_in].should == :group
>   end
> end

Yes, I got this to work too but not for nested describes as below:

describe "group options hash", :defined_in => :group do
   ..
   describe "nested describe wont work"
    it "is available in examples via self.class.options" do
     self.class.options[:defined_in].should == :group # WONT work
    end
   end

end

BTW. I am actually accessing the hash from within a common helper
files in a before action setup by Spec::Runner.configure. Hence it is
important that accessing the Hash work regardless of how the describes
are nested.
B1789b17a54c1731ce9fe4a6480d24e4?d=identicon&s=25 mortench (Guest)
on 2009-05-13 22:12
(Received via mailing list)
On 13 Maj, 01:14, Randy Harmon <r_j_h_box...@yahoo.com> wrote:
> mortench wrote:
> > The new rspec beta book and the rspec site mentions that it is
> > possible to add a hash to "describe" and "it" constructs but very,
> > very litle information about the particulars.
>
> > What can the options hash be used for and what should it be used for?
>
> This might be used for tagging examples, marking examples for use with
> formatters, or other purposes probably not bearing on spec execution.

"not bearing on spec execution". I.e. for meta-data only. Any official
word on thos?

> I'm not saying they could or couldn't be used the way you're suggesting,
> but there are other possibilities you should try first IMO.

Ok. Thanks for the advice.

> I have used a callback for this, but I understand that rspec 1.2 has
> macros that can take args the way your shared example can't.   I haven't
> dug into them.

Ok. will look into that.

>   it_should_behave_like "shared"
>   def shared_options
>     { :foo => 'bar', :baz => 'buz }
>   end
> end

Thanks for the suggestion. I can see how it world work, but for this
particular problem I have (where 3parties would need to reuse my
tests),  I would like something that not only works but is also simple
and elegant....

And the above is a bit verbose I think compared to just

describe "thingy",  :foo => 'bar', :baz => 'buz
...
end
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-27 10:40
(Received via mailing list)
On Wed, May 13, 2009 at 3:36 PM, mortench <mortench@gmail.com> wrote:
>
> BTW. I am actually accessing the hash from within a common helper
> files in a before action setup by Spec::Runner.configure. Hence it is
> important that accessing the Hash work regardless of how the describes
> are nested.

Just catching up on some old mail here - would you please add a
feature request for this? http://rspec.lighthouseapp.com
This topic is locked and can not be replied to.