Read more

How to find out if you are in Cucumber or in RSpec

Arne Hartherz
May 08, 2013Software engineer at makandra GmbH

Sometimes you need a piece of code to do something different for specs than for features. If you don't have separate environments, you can't check your Rails.env.

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

I managed to distinguish between specs and features by asking Capybara.

Note that this only works when you do not use Capybara in specs.

if defined?(Capybara) and Capybara.respond_to?(:current_driver)
  # you're in a Cucumber scenario
else
  # you're probably in a spec
end

You could omit the defined?(Capybara) condition, if you are sure that Capybara is always defined when that code is being called.

Posted by Arne Hartherz to makandra dev (2013-05-08 15:59)