Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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
RSpec::Core::ExampleGroup.describe.

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:

sinatra/sinatra@46bdb7d
  • Loading branch information...
commit b45c10e0d9d67595d62838ac79d803f5786f6175 1 parent 18903e1
Myron Marston myronmarston authored
2  Changelog.md
View
@@ -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
7 lib/rspec/core/dsl.rb
View
@@ -21,4 +21,9 @@ def describe(*args, &example_group_block)
end
end
-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)
17 spec/rspec/core/dsl_spec.rb
View
@@ -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
+ Module.new.should respond_to(:describe)
+ end
+
+ it 'is not available on other types of objects' do
+ Object.new.should_not respond_to(:describe)
+ end
+end
+
3  spec/rspec/core/example_group_spec.rb
View
@@ -863,6 +863,9 @@ def metadata_hash(*args)
end
end
+ extend Forwardable
+ def_delegators "RSpec::Core::ExampleGroup", :describe
+
context "with all examples passing" do
it "returns true" do
group = describe("something") do
Please sign in to comment.
Something went wrong with that request. Please try again.