1

In my RSpec file, I have used FactoryGirl to create objects that I have used in my RSpec file. In my RSpec file I have used three objects A, B and C where C is dependent on B and B is dependent on A. Here in RSpec file I have written two test cases: one for testing index method of my controller and another for update method. So I have created the objects which I have used to test those two methods, as follows:

before(:each) do
  @A = FactoryGirl.create(:A)
  @B = FactoryGirl.create(:B)
  @C = FactoryGirl.create(:C)
end

And here I am using test environment of Rails, so I am using corresponding test database which I have already. So to clean the database after running, I have written the following code:

 after(:each) do
   C.all.destroy
   B.all.destroy
   A.all.destroy
 end

But the problem is that after running the spec, while I am checking the tables corresponding to them I find that data is not deleted. Here I have used DataMapper in my models. So can please anyone help me to fix this problem i.e. to clean those tables after each run of spec. Thank you.

Joy
  • 4,197
  • 14
  • 61
  • 131
  • use transactional fixtures! (setting in your `spec_helper.rb` file). Or use database_cleaner for those pesky specs that require multiple threads (and thus can't use db transactions safely). – sevenseacat Aug 22 '13 at 03:00

1 Answers1

0

You should use database cleaner gem, it helps you to run your specs in a transaction so your database is cleaned between each example. It also can be configured to run specific examples with deletion/truncation so you can use it with external tools (selenium or other drivers)

It's all explained right there:

https://github.com/bmabey/database_cleaner

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • But by reading it and from a few links from google I did not find how to use DataMapper with DataBase Cleaner gem, the articles mostly deal with ActiveRecord. – Joy Aug 22 '13 at 04:13