From 4b3bef302d3e748629ef61140e73fb8fc22c6aae Mon Sep 17 00:00:00 2001 From: Alexis Montagne Date: Sun, 8 Jun 2014 14:20:04 +0200 Subject: [PATCH] Could forward metadata to the generated it block --- lib/rspec/its.rb | 23 +++++++++++++++++++++-- spec/rspec/its_spec.rb | 8 ++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/rspec/its.rb b/lib/rspec/its.rb index 9ca7909..b72e270 100644 --- a/lib/rspec/its.rb +++ b/lib/rspec/its.rb @@ -67,6 +67,25 @@ module Its # its(:size) { is_expected.to eq(0) } # end # + # You can pass more than one arguments on the `its` block to add + # some options to the generated example + # + # @example + # + # # This ... + # describe Array do + # its(:size, :focus) { should eq(0) } + # end + # + # # ... generates the same runtime structure as this: + # describe Array do + # describe "size" do + # it "should eq(0)", :focus do + # subject.size.should eq(0) + # end + # end + # end + # # Note that this method does not modify `subject` in any way, so if you # refer to `subject` in `let` or `before` blocks, you're still # referring to the outer subject. @@ -78,7 +97,7 @@ module Its # before { subject.age = 25 } # its(:age) { should eq(25) } # end - def its(attribute, &block) + def its(attribute, *options, &block) describe(attribute.to_s) do if Array === attribute let(:__its_subject) { subject[*attribute] } @@ -104,7 +123,7 @@ def should_not(matcher=nil, message=nil) end its_caller = caller.select {|file_line| file_line !~ %r(/lib/rspec/its) } - example(nil, :caller => its_caller, &block) + example(nil, *options, :caller => its_caller, &block) end end diff --git a/spec/rspec/its_spec.rb b/spec/rspec/its_spec.rb index 170e15a..900c96a 100644 --- a/spec/rspec/its_spec.rb +++ b/spec/rspec/its_spec.rb @@ -22,6 +22,14 @@ def call_count end.new end + before(:each, :meta) do + subject.call_count + end + + context "with some metadata" do + its(:call_count, :meta) { should eq(2) } + end + context "with a call counter" do its(:call_count) { should eq(1) } end