[rspec-users] Options Hash explanation ?

Randy Harmon r_j_h_box-sf at yahoo.com
Tue May 12 19:14:42 EDT 2009


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




More information about the rspec-users mailing list