Nested Example Groups

November 29th, 2007

Since rspec first appeared on the scene, users have been asking for nested example groups. Well it has finally arrived. RSpec 1.1.0 will ship with support for nesting, so you’ll be able to do things like this:


describe RSpec do
  before(:each) do
    @rspec = RSpec.new
  end

describe "at release 1.0.8" do before(:each) do @rspec.version = "1.0.8" end

it "should not support nested example groups" do
  @rspec.should_not support_nested_example_groups
end

end

describe “at release 1.1.0″ do before(:each) do @rspec.version = “1.1.0″ end

it "should support nested example groups" do
  @rspec.should support_nested_example_groups
end

end end

This will output:

RSpec at release 1.0.8
- should not support nested example groups

RSpec at release 1.1.0 - should support nested example groups

If you’re using trunk, you can do this now with revision 3009 or later.

<p>Happy nesting!</p>

10 Responses to “Nested Example Groups”

  1. Tom Ward Says:

    This looks great. Very much appreciated.

  2. Matthijs Langenberg Says:

    Whoah! Great feature! It’s going to make my controller examples more DRY!

  3. CN Says:

    When is the next release 1.1.0?

    <p>I am retesting my applications with rails RC_2 now and there are lots of things to change. Is rspec compatible already with the release.</p>
    
  4. Lance Carlson Says:

    This really helps with organization. Thanks for this update David!

  5. Dan Kubb Says:

    Thank you very much! This is probably the feature I most wanted to help better organize my specs. Many of my specs have similar setup in the before block, and then diverge slightly. Previously I had been creating a module with setup methods, and them import it into every describe block, but this is going to show my intentions much more clearly.

  6. David Chelimsky Says:

    @CN – we’ll be doing a 1.1.0-RC1 within a week. We still have some work to do but we want to get it out the door.

    <p>In the mean time, the current trunk (3015) IS compatible w/ Rails 2.0.0-RC2.</p>
    
    
    <p>@Matthijs &#8211; sadly, <a href="http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/156">it doesn&#8217;t work that well w/ controller examples</a>. It will before the final 1.1.0 release, but not necessarily before the RC.</p>
    
  7. Sean Miller Says:

    This is terrific. Any chance of getting the nesting to appear as indenting in the html report output? When we introduced rspec for acceptance-test-driven-development in the summer, the one thing that would have made our PM even happier would have been if the html report could have subsections indented, and sub-sub-sections doubly indented, so he could show the whole project’s rspec_report.html to the rest of the business and orient them in it more quickly.

  8. David Chelimsky Says:

    @Sean – our focus for business-facing reporting is on the story runner. Check out the prototype for the in-browser story runner/editor if you haven’t already

    <p>The addition of nesting is really a developer-friendly addition in that it supports DRYing up your examples, but it doesn&#8217;t really affect output yet.</p>
    
    
    <p>We may eventually support some different output that is more aligned w/ the story runner output, but that is very low on our priority list right now. In fact, it&#8217;s not even in the tracker &#8211; so feel free to <a href="http://rspec.lighthouseapp.com">add a feature request</a>.</p>
    
  9. Zach Dennis Says:

    Thank you so much David! This is a feature I’ve been waiting for!

  10. CN Says:

    David, thanks for your reply I am a great fan of the tool. Turned our to be problems with ruby gems and not rspec in specific. Mongrel 1.1.1 is now also complaining when booting that some plugins cannot be found. looking forward for the release…