DerNalia
DerNalia

Reputation: 327

Ruby on Rails: Cucumber: how to reset the db after each scenario?

So I am in my test environment

now, in the terminial, rake db:test:prepare clears the db... but not when i run it from the code

And I have this in features/support/env.rb:

Before do
    task :build_all do
      [ :debug, :release ].each do |t|
        $build_type = t
        Rake::Task["db:test:prepare"].reenable
        Rake::Task["db:test:prepare"].invoke
      end
    end
end

But my data remains in the project_test db when my tests are done running

This is in my database.yml

test:
  adapter: mysql
  encoding: utf8
  database: projectname_test
  username: root
  password:

Ive also tried

db:test:purge

and

db:test:reset

and I know that it is using my test db, because I check mySQLWorkbench, and it inserts data into the tables... but doesn't delete the data when its done (I have to delete it manually). When the tables are empty, the test cases pass

Upvotes: 7

Views: 6556

Answers (3)

vijay chouhan
vijay chouhan

Reputation: 1012

You should use

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner.strategy = :truncation

rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."

end

Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end

Upvotes: 4

Ryan Bigg
Ryan Bigg

Reputation: 107738

Scenarios in Cucumber, like tests in RSpec, are run in transactions blocks and are rolled back once the scenario is done. Any data that's in the database that shouldn't be there is probably left over from something else. Try purging your database.

Upvotes: 1

Nik
Nik

Reputation: 2484

Idk about this, but have you tried dropping and recreating the database on the fly?

Upvotes: 0

Related Questions