Browse files

Add "options" accessor for running example

This allows custom behavior in before blocks that can be triggered by
passing in arbitrary metadata to the "it" method when setting up the
example:

  it 'should be special', :special => true do
    ...
  end

The custom behavior can be set-up, for example, using config.before:

  RSpec.configure do |config|
    config.before :each do
      special_setup if example.options[:special]
    end
  end

Closes #42.
  • Loading branch information...
1 parent 7366e35 commit 48f1d5d034b27fdd9aca514fab9f9ecec5847895 @wincent wincent committed with dchelimsky Jun 17, 2010
Showing with 47 additions and 1 deletion.
  1. +1 −1 lib/rspec/core/example.rb
  2. +36 −0 spec/rspec/core/example_group_spec.rb
  3. +10 −0 spec/rspec/core/example_spec.rb
View
2 lib/rspec/core/example.rb
@@ -2,7 +2,7 @@ module RSpec
module Core
class Example
- attr_reader :metadata, :example_block
+ attr_reader :metadata, :example_block, :options
def self.delegate_to_metadata(*keys)
keys.each do |key|
View
36 spec/rspec/core/example_group_spec.rb
@@ -268,6 +268,42 @@ module RSpec::Core
example.metadata[:execution_result][:exception_encountered].message.should == "error in before all"
end
end
+
+ it "has no 'running example' within before(:all)" do
+ group = ExampleGroup.describe
+ running_example = :none
+ group.before(:all) { running_example = example }
+ group.example("no-op") { }
+ group.run_all
+ running_example.should == nil
+ end
+
+ it "has access to example options within before(:each)" do
+ group = ExampleGroup.describe
+ option = nil
+ group.before(:each) { option = example.options[:data] }
+ group.example("no-op", :data => :sample) { }
+ group.run_all
+ option.should == :sample
+ end
+
+ it "has access to example options within after(:each)" do
+ group = ExampleGroup.describe
+ option = nil
+ group.after(:each) { option = example.options[:data] }
+ group.example("no-op", :data => :sample) { }
+ group.run_all
+ option.should == :sample
+ end
+
+ it "has no 'running example' within after(:all)" do
+ group = ExampleGroup.describe
+ running_example = :none
+ group.after(:all) { running_example = example }
+ group.example("no-op") { }
+ group.run_all
+ running_example.should == nil
+ end
end
describe "adding examples" do
View
10 spec/rspec/core/example_spec.rb
@@ -25,6 +25,10 @@
it "should have one for its block" do
example_instance.should respond_to(:example_block)
end
+
+ it "should have one for its options" do
+ example_instance.should respond_to(:options)
+ end
end
describe '#inspect' do
@@ -56,6 +60,12 @@
end
end
+ describe "accessing options within a running example" do
+ it "should be able to look up option values by key", :demo => :data do
+ example.options[:demo].should == :data
+ end
+ end
+
describe "#run" do
it "runs after(:each) when the example passes" do
after_run = false

0 comments on commit 48f1d5d

Please sign in to comment.