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 money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
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)