Read more

Run your own code before specific RSpec examples

Thomas Eisenbarth
February 21, 2012Software engineer at makandra GmbH

You probably know about the possibility to tag scenarios in Cucumber to run your own piece of code before the actual scenario is run which looks like that:

@foo
Scenario: Do something
...
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

and you place the following snippet into support/env.rb:

Before('@foo') do
  puts "This is run every time a @foo tagged scenario is hit"
end    

You can tag RSpec examples like this:

it 'does something', :foo => true do
  ...
end

What you need is the following within the RSpec.configure do |config| block within spec_helper.rb

config.before(:each, :foo => true) do
  puts "This is run every time a tagged rspec example is hit"
end
Posted by Thomas Eisenbarth to makandra dev (2012-02-21 17:46)