Skip to content

Commit

Permalink
Pass parameters given to #it_should_behave_like on to the shared exam…
Browse files Browse the repository at this point in the history
…ple group.
  • Loading branch information
myronmarston committed Aug 1, 2010
1 parent cc72146 commit c353bad
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
32 changes: 32 additions & 0 deletions features/example_groups/shared_example_group.feature
Expand Up @@ -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:
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/example_group.rb
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/rspec/core/shared_example_group_spec.rb
Expand Up @@ -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 = []
Expand Down

0 comments on commit c353bad

Please sign in to comment.