Nested Example Groups
November 28th, 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.
Happy nesting!
10 Responses to “Nested Example Groups”
Sorry, comments are closed for this article.


July 13th, 2008 at 06:06 AM
This looks great. Very much appreciated.
July 13th, 2008 at 06:06 AM
Whoah! Great feature! It’s going to make my controller examples more DRY!
July 13th, 2008 at 06:06 AM
When is the next release 1.1.0?
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.
July 13th, 2008 at 06:06 AM
This really helps with organization. Thanks for this update David!
July 13th, 2008 at 06:06 AM
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.
July 13th, 2008 at 06:06 AM
@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.
In the mean time, the current trunk (3015) IS compatible w/ Rails 2.0.0-RC2.
@Matthijs – sadly, it doesn’t work that well w/ controller examples. It will before the final 1.1.0 release, but not necessarily before the RC.
July 13th, 2008 at 06:06 AM
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.
July 13th, 2008 at 06:06 AM
@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
The addition of nesting is really a developer-friendly addition in that it supports DRYing up your examples, but it doesn’t really affect output yet.
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’s not even in the tracker – so feel free to add a feature request.
July 13th, 2008 at 06:06 AM
Thank you so much David! This is a feature I’ve been waiting for!
July 13th, 2008 at 06:06 AM
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…