Browse files

Don't add `describe` to every object.

Instead, make it only available on:
  - The main object--so it can be used at the top level.
  - Modules--so example groups can be declared nested
    within modules, as is the common practice.

Besides this, the only other place we need describe is from within
example groups (so we can nest them), and this is taken care of by

I got the idea for this from a recent change in Sinatra that similarly
limits the DSL to just the main object rather than all objects:

  • Loading branch information...
1 parent 18903e1 commit b45c10e0d9d67595d62838ac79d803f5786f6175 @myronmarston myronmarston committed Jun 16, 2012
Showing with 28 additions and 1 deletion.
  1. +2 −0
  2. +6 −1 lib/rspec/core/dsl.rb
  3. +17 −0 spec/rspec/core/dsl_spec.rb
  4. +3 −0 spec/rspec/core/example_group_spec.rb
@@ -12,6 +12,8 @@ Enhancements
for background.
* thanks to Bradley Schaefer for suggesting it and Avdi Grimm for almost
suggesting it.
+* Limit monkey patching of `describe` onto just the objects
+ that need it rather than every object in the system (Myron Marston).
Bug fixes
@@ -21,4 +21,9 @@ def describe(*args, &example_group_block)
-include RSpec::Core::DSL
+# make describe available on the main object, but not all objects
+extend RSpec::Core::DSL
+# make describe available on modules so example groups
+# can be nested within them.
+Module.send(:include, RSpec::Core::DSL)
@@ -0,0 +1,17 @@
+require 'spec_helper'
+main = self
+describe "The describe method" do
+ it 'is available on the main object' do
+ main.should respond_to(:describe)
+ end
+ it 'is available on modules (so example groups can be nested inside them)' do
+ respond_to(:describe)
+ end
+ it 'is not available on other types of objects' do
+ respond_to(:describe)
+ end
@@ -863,6 +863,9 @@ def metadata_hash(*args)
+ extend Forwardable
+ def_delegators "RSpec::Core::ExampleGroup", :describe
context "with all examples passing" do
it "returns true" do
group = describe("something") do

0 comments on commit b45c10e

Please sign in to comment.