Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 528
DerNalia Avatar asked Jun 15 '10 23:06

DerNalia


2 Answers

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
like image 85
vijay chouhan Avatar answered Nov 03 '22 13:11

vijay chouhan


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.

like image 40
Ryan Bigg Avatar answered Nov 03 '22 12:11

Ryan Bigg