From c353badcb8154ab98a7dc46eb19c8a9fc702ec73 Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Sun, 1 Aug 2010 15:16:29 -0700 Subject: [PATCH] Pass parameters given to #it_should_behave_like on to the shared example group. --- .../shared_example_group.feature | 32 +++++++++++++++++++ lib/rspec/core/example_group.rb | 4 +-- spec/rspec/core/shared_example_group_spec.rb | 20 ++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/features/example_groups/shared_example_group.feature b/features/example_groups/shared_example_group.feature index 09a2f8a156..4b1b4d5a36 100644 --- a/features/example_groups/shared_example_group.feature +++ b/features/example_groups/shared_example_group.feature @@ -96,6 +96,38 @@ Feature: Shared example group adds objects to the end of the collection """ + Scenario: Passing parameters to a shared example group + Given a file named "shared_example_group_params_spec.rb" with: + """ + shared_examples_for "a measurable object" do |measurement_method, measurement| + it "should return #{measurement} from ##{measurement_method}" do + subject.send(measurement_method).should == measurement + end + end + + describe Array, "with 3 items" do + subject { [1, 2, 3] } + it_should_behave_like "a measurable object", :size, 3 + end + + describe String, "of 6 characters" do + subject { "FooBar" } + it_should_behave_like "a measurable object", :length, 6 + end + """ + When I run "rspec shared_example_group_params_spec.rb --format documentation" + Then the output should contain "2 examples, 0 failures" + And the output should contain: + """ + Array with 3 items + it should behave like a measurable object + should return 3 from #size + + String of 6 characters + it should behave like a measurable object + should return 6 from #length + """ + Scenario: Aliasing "it_should_behave_like" to "it_has_behavior" Given a file named "shared_example_group_spec.rb" with: """ diff --git a/lib/rspec/core/example_group.rb b/lib/rspec/core/example_group.rb index 15e775ccb8..205096daf3 100644 --- a/lib/rspec/core/example_group.rb +++ b/lib/rspec/core/example_group.rb @@ -60,12 +60,12 @@ class << self def self.define_shared_group_method(new_name, report_label=nil) module_eval(<<-END_RUBY, __FILE__, __LINE__) - def self.#{new_name}(name, &customization_block) + 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 describe("#{report_label || "it should behave like"} \#{name}") do - module_eval &shared_block + module_exec *args, &shared_block module_eval &customization_block if customization_block end end diff --git a/spec/rspec/core/shared_example_group_spec.rb b/spec/rspec/core/shared_example_group_spec.rb index b1374d56db..27aa696cd1 100644 --- a/spec/rspec/core/shared_example_group_spec.rb +++ b/spec/rspec/core/shared_example_group_spec.rb @@ -80,6 +80,26 @@ def self.foo; end 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_all + + passed_params.should == { :param1 => :value1, :param2 => :value2 } + end + end + context "given a block" do it "evaluates the block in nested group" do scopes = []