1

I am having trouble in cleaning database between features. I tried using Before hooks but it runs for each scenario but I only need to clean database at the start of each feature and not between scenarios.

Any suggestions would be helpful.

Srani
  • 11
  • 1
  • 3

5 Answers5

0

I use DatabaseCleaner https://github.com/DatabaseCleaner/database_cleaner I'm satisfed

  config.before(:each) do |spec|
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.start
    ...
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

in rails_herper.rb

Safi Nettah
  • 1,160
  • 9
  • 15
  • I have tried this. The problem is that once I run cucumber it gets loaded only once and then all the features start running. But what I need is to clean DB in between each feature. How can that be achieved? – Srani Mar 23 '18 at 13:29
  • Also I need it for cucumber and not rspec – Srani Mar 23 '18 at 13:33
  • You should be able to use an After hook? – Marit Mar 23 '18 at 13:51
  • An After hook runs the cleaner for each scenario. And I don't want that, I need to run before each feature file in cucumber – Srani Mar 23 '18 at 14:01
0

Here is sample config

before(:all) do
  DatabaseCleaner.clean   
end

In RSpec, you have tags, types, hooks etc.. In your case easiest way will be adding before(: all) in each file. This will start cleaning before all tests in described context.

From documentation Rspec Docs

before(:all) blocks are run once before all of the examples in a group

Argonus
  • 1,005
  • 6
  • 11
0

The database should be cleaned before every scenario as Cucumber intends. Stopping Cucumber from doing this is a false optimisation, and a common anti-pattern followed by lots of less experienced Cucumber users. Scenarios should never be dependent on each other.

To get this to work remove any code you have added to your application to restrict how cucumber cleans the database.

If you are not sure how to do this create a new rails project using the same ruby and rails versions you are using and then add the cucumber-rails gem. It will setup everything as intended. You can use the diff of before/after cucumber-rails to compare.

diabolist
  • 3,990
  • 1
  • 11
  • 15
  • Point taken. But right now my scenarios are dependent on each other and if the database is getting cleared in between the scenarios my tests will fail. Is there any possible way I could use the DB cleaner in between features? – Srani Mar 25 '18 at 15:45
  • Not that I know. I strongly recommend you rewrite your scenarios. Take this opportunity to simplify and learn, connected scenarios will only cause you continuing problems as your project progresses. – diabolist Mar 25 '18 at 23:15
0

You can clean database before (not after) each scenario with the following code. Just add it to your features/support/env.rb

Cucumber::Rails::Database.autorun_database_cleaner = false
DatabaseCleaner.strategy = :truncation
Cucumber::Rails::Database.javascript_strategy = :truncation

Before do
  DatabaseCleaner.clean
end
Hirurg103
  • 4,783
  • 2
  • 34
  • 50
0

Just a work around/hack, just in case you haven't found a solution yet. The trick here is to use tagged cucumber hooks!

Provide a tag like @LastScenario in your last scenario in the feature file/s. Then using the @After hook of cucumber perform the action, say data clean up in your case. Something like: @LastScenario Scenario: My Scenario Name Given I have something...

And then in the Hooks.java Class:

public class Hooks {

@After("@LastScenario")
public void dataCleanUp() {

    CleanUpScripts cleanUpScripts = new CleanUpScripts();
    cleanUpScripts.dataCleanUp();
}

}

Same can be done using @Before Hook as well - based on what is required.

Sam
  • 181
  • 2
  • 3
  • 13