3

I am using rspec to test and I notice that after running the tests, it sometimes leaves some records in the test database. I'm not sure why. I have use_transactional_fixtures set to true in my config file. But they don't go away until I manually delete the records. Does anyone have a way to stop this?

EDIT; before i said it was only when tests fail. that's no longer true.

bdwain
  • 1,665
  • 16
  • 35

2 Answers2

6

I found the problem. Before(:all) blocks are not transactional

bdwain
  • 1,665
  • 16
  • 35
  • Better source from [documentation](https://www.relishapp.com/rspec/rspec-rails/v/3-5/docs/transactions): *Data created in `before(:all)` are not rolled back*. – Franklin Yu Jan 22 '17 at 10:39
-1

Try database_cleaner gem.

Truncation or Transaction strategy will work for you.

Caution: It can make your test suite run terribly slow.
Normally, proper use before, after in RSpec(if you are using it) works usually fine.

kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • 1
    What do you mean proper use of before and after? What would be improper use? it's not a problem i see every time. I just don't know what would cause it to leave records around, so I'd like to stop doing that rather than adding a new gem. – bdwain Apr 28 '13 at 23:54
  • 1
    This is the thing with the test suite - no matter what we think, data does persist in database, when it shouldn't. I've practically spent days trying to make the factories work. Last time, I was forced to use this gem, because of this. Though it did the job, perfectly. :) – kiddorails Apr 29 '13 at 17:41
  • I think if you avoid creating things outside of it blocks and before/after/around(:all) (and anything else that doesn't run per example, you should be good – bdwain Apr 29 '13 at 19:15