Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

delegate shared content search to single method and align error messages

- #432.
  • Loading branch information...
commit 940f7feb822543737e58fe1bf73ca2bdfda0583d 1 parent 3f27e7a
@dchelimsky dchelimsky authored
View
20 lib/rspec/core/example_group.rb
@@ -68,8 +68,8 @@ class << self
def self.define_nested_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block)
- shared_block = world.shared_example_groups[name]
- raise "Could not find shared example group named \#{name.inspect}" unless shared_block
+ shared_block = find_shared("examples", name)
+ raise "Could not find shared examples \#{name.inspect}" unless shared_block
group = describe("#{report_label || "it should behave like"} \#{name}") do
module_eval_with_args(*args, &shared_block)
@@ -90,17 +90,21 @@ class << self
alias_it_should_behave_like_to :it_behaves_like, "behaves like"
def self.include_context(name)
+ module_eval(&find_shared("context", name))
+ end
+
+ def self.include_examples(name)
+ module_eval(&find_shared("examples", name))
+ end
+
+ def self.find_shared(label, name)
if world.shared_example_groups.has_key?(name)
- module_eval(&world.shared_example_groups[name])
+ world.shared_example_groups[name]
else
- raise ArgumentError, "could not find shared context #{name.inspect}"
+ raise ArgumentError, "Could not find shared #{label} #{name.inspect}"
end
end
- class << self
- alias_method :include_examples, :include_context
- end
-
def self.examples
@examples ||= []
end
View
120 spec/rspec/core/example_group_spec.rb
@@ -894,8 +894,9 @@ def foo; 'foo'; end
ExampleGroup.describe do
include_context "shared stuff"
end
- end.to raise_error(ArgumentError,%q|could not find shared context "shared stuff"|)
+ end.to raise_error(ArgumentError,%q|Could not find shared context "shared stuff"|)
end
+
end
describe "#include_examples" do
@@ -912,7 +913,124 @@ def foo; 'foo'; end
end
group.examples.first.description.should eq("does something")
end
+
+ it "raises a helpful error message when shared context is not found" do
+ expect do
+ ExampleGroup.describe do
+ include_examples "shared stuff"
+ end
+ end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|)
+ end
end
+ describe "#it_should_behave_like" do
+ it "creates a nested group" do
+ shared_examples_for("thing") {}
+ group = ExampleGroup.describe('fake group')
+ group.it_should_behave_like("thing")
+ group.should have(1).children
+ end
+
+ it "creates a nested group for a class" do
+ klass = Class.new
+ shared_examples_for(klass) {}
+ group = ExampleGroup.describe('fake group')
+ group.it_should_behave_like(klass)
+ group.should have(1).children
+ end
+
+ it "adds shared examples to nested group" do
+ shared_examples_for("thing") do
+ it("does something")
+ end
+ group = ExampleGroup.describe('fake group')
+ shared_group = group.it_should_behave_like("thing")
+ shared_group.should have(1).examples
+ end
+
+ it "adds shared instance methods to nested group" do
+ shared_examples_for("thing") do
+ def foo; end
+ end
+ group = ExampleGroup.describe('fake group')
+ shared_group = group.it_should_behave_like("thing")
+ shared_group.public_instance_methods.map{|m| m.to_s}.should include("foo")
+ end
+
+ it "adds shared class methods to nested group" do
+ shared_examples_for("thing") do
+ def self.foo; end
+ end
+ group = ExampleGroup.describe('fake group')
+ shared_group = group.it_should_behave_like("thing")
+ shared_group.methods.map{|m| m.to_s}.should include("foo")
+ end
+
+ context "given some parameters" do
+ it "passes the parameters to the shared example group" do
+ passed_params = {}
+
+ shared_examples_for("thing") do |param1, param2|
+ it("has access to the given parameters") do
+ passed_params[:param1] = param1
+ passed_params[:param2] = param2
+ end
+ end
+
+ group = ExampleGroup.describe("group") do
+ it_should_behave_like "thing", :value1, :value2
+ end
+ group.run
+
+ passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
+ end
+
+ it "adds shared instance methods to nested group" do
+ shared_examples_for("thing") do |param1|
+ def foo; end
+ end
+ group = ExampleGroup.describe('fake group')
+ shared_group = group.it_should_behave_like("thing", :a)
+ shared_group.public_instance_methods.map{|m| m.to_s}.should include("foo")
+ end
+
+ it "evals the shared example group only once" do
+ eval_count = 0
+ shared_examples_for("thing") { |p| eval_count += 1 }
+ group = ExampleGroup.describe('fake group')
+ shared_group = group.it_should_behave_like("thing", :a)
+ eval_count.should eq(1)
+ end
+ end
+
+ context "given a block" do
+ it "evaluates the block in nested group" do
+ scopes = []
+ shared_examples_for("thing") do
+ it("gets run in the nested group") do
+ scopes << self.class
+ end
+ end
+ group = ExampleGroup.describe("group") do
+ it_should_behave_like "thing" do
+ it("gets run in the same nested group") do
+ scopes << self.class
+ end
+ end
+ end
+ group.run
+
+ scopes[0].should be(scopes[1])
+ end
+ end
+
+ it "raises a helpful error message when shared context is not found" do
+ expect do
+ ExampleGroup.describe do
+ it_should_behave_like "shared stuff"
+ end
+ end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|)
+ end
+ end
end
end
View
108 spec/rspec/core/shared_example_group_spec.rb
@@ -61,114 +61,6 @@ module RSpec::Core
end
end
- describe "#it_should_behave_like" do
- it "creates a nested group" do
- shared_examples_for("thing") {}
- group = ExampleGroup.describe('fake group')
- group.it_should_behave_like("thing")
- group.should have(1).children
- end
-
- it "creates a nested group for a class" do
- shared_examples_for(ExampleClass) {}
- group = ExampleGroup.describe('fake group')
- group.it_should_behave_like(ExampleClass)
- group.should have(1).children
- end
-
- it "adds shared examples to nested group" do
- shared_examples_for("thing") do
- it("does something")
- end
- group = ExampleGroup.describe('fake group')
- shared_group = group.it_should_behave_like("thing")
- shared_group.should have(1).examples
- end
-
- it "adds shared instance methods to nested group" do
- shared_examples_for("thing") do
- def foo; end
- end
- group = ExampleGroup.describe('fake group')
- shared_group = group.it_should_behave_like("thing")
- shared_group.public_instance_methods.map{|m| m.to_s}.should include("foo")
- end
-
- it "adds shared class methods to nested group" do
- shared_examples_for("thing") do
- def self.foo; end
- end
- group = ExampleGroup.describe('fake group')
- shared_group = group.it_should_behave_like("thing")
- shared_group.methods.map{|m| m.to_s}.should include("foo")
- end
-
- context "given some parameters" do
- it "passes the parameters to the shared example group" do
- passed_params = {}
-
- shared_examples_for("thing") do |param1, param2|
- it("has access to the given parameters") do
- passed_params[:param1] = param1
- passed_params[:param2] = param2
- end
- end
-
- group = ExampleGroup.describe("group") do
- it_should_behave_like "thing", :value1, :value2
- end
- group.run
-
- passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
- end
-
- it "adds shared instance methods to nested group" do
- shared_examples_for("thing") do |param1|
- def foo; end
- end
- group = ExampleGroup.describe('fake group')
- shared_group = group.it_should_behave_like("thing", :a)
- shared_group.public_instance_methods.map{|m| m.to_s}.should include("foo")
- end
-
- it "evals the shared example group only once" do
- eval_count = 0
- shared_examples_for("thing") { |p| eval_count += 1 }
- group = ExampleGroup.describe('fake group')
- shared_group = group.it_should_behave_like("thing", :a)
- eval_count.should eq(1)
- end
- end
-
- context "given a block" do
- it "evaluates the block in nested group" do
- scopes = []
- shared_examples_for("thing") do
- it("gets run in the nested group") do
- scopes << self.class
- end
- end
- group = ExampleGroup.describe("group") do
- it_should_behave_like "thing" do
- it("gets run in the same nested group") do
- scopes << self.class
- end
- end
- end
- group.run
-
- scopes[0].should be(scopes[1])
- end
- end
-
- it "raises when named shared example_group can not be found" do
- group = ExampleGroup.describe("example_group")
- lambda do
- group.it_should_behave_like("a group that does not exist")
- end.should raise_error(/Could not find shared example group named/)
- end
- end
-
describe "#share_as" do
it "is exposed to the global namespace" do
Kernel.should respond_to("share_as")
Please sign in to comment.
Something went wrong with that request. Please try again.