By default your specs and cucumber feature both run in the test rails environemnt i.e. RAILS_ENV=test.
However, I need these two environments to be different. First I’ll show you how, then I’ll tell you why.
I want to have my feature run in a cucumber environment i.e. RAILS_ENV=cucumber. Simply edit the features/support/env.rb file and update the default environment value so that you file looks like this:
ENV['RAILS_ENV'] = 'cucumber' Rails.env = 'cucumber'
We still want our feature to hit the test database, so simply create a pointer in your database.yml file to do this:
test: &TEST adapter: ... database: .... host: ... cucumber: <<: *TEST
Lastly, make sure you have a config/environments/cucumber.rb file for your new cucumber environment. I normally just copy the test environment file.
Now when you run your features they will run in their own, separate environment. Groovy.
Optional: You may also want to modify your Gemfile by adding an additional :cucumber group. I’ve never done this and never run into any problems, but bear it in mind.
Why do I need this?
When I run my specs I use spork and autotest. This makes running specs lightning fast. However, you do need to make some tweaks to the test.rb environment file, namely turning class caching off. This is to make sure your specs always run with the latest saved versions of your models.
config.cache_classes = false
For cucumber, you can leave caching on. However, I use database_cleaner for my features with a truncation strategy. This makes the running of selenium features reliable because no transaction has to be rolled back after each scenario – the data is simply truncated.