Well all know about before_each and after_each but you can also wrap your tests in blocks for VCR cassettes or proxying requests.

This can be accomplished within your spec_helper.rb or a specific file.

Examples

Within Rspec.config spec_helper.rb:

...
config.treat_symbols_as_metadata_keys_with_true_values = true
config.around(:each, :two_times) do |example|
  2.times do
    example.call
  end
end
...

Tests are wrapped when :two_times is passed in.

...
it "confirms the obvious", :two_times do
  15.should == 15
end
...

Within a spec file:

...
describe "around filter" do
  around(:each) do |example|
    puts "around each before"
    example.run
    puts "around each after"
  end

  it "gets run in order" do
    puts "in the example"
  end
end
...

Comments