当前位置: 动力学知识库 > 问答 > 编程问答 >

Why is my test database empty before and after running rails test?

问题描述:

Although most people want their database empty between runs of a unit/functional test, I'm debugging a problem and find it inconvenient and rather surprising that my test database is empty before and after running a test. Even if I put something in the setup and teardown to print out the size of a particular table, it is always 0 on setup, and sometimes more than 0 on teardown.

What is doing this for me? I have no fixtures (I have commented out the default "fixtures :all" in test_helper) and I am using simple unit/functional tests that extend ActiveSupport::TestCase (no RSpec or anything like that). I have not added much/any custom configuration/setup since generating the rails project.

I am running Rails 3.0.9 and Ruby 1.8.7.

All ideas welcome!

网友答案:

You can disable this behavior by setting use_transactional_fixtures to false:

class MyTest < ActiveSupport::TestCase
  self.use_transactional_fixtures = false
end 
网友答案:

Unit tests need to have known, fresh state, which is why the DB gets cleared.

Preventing test db from getting wiped is pretty easy w/ RSpec. Not sure how you'd do it in the context of Rails' built-in Test::Unit stuff, but one thing you might want to consider if this is just for quick debugging purposes is simply using the logger and tailing the test log:

Rails.logger.info "\n\nThe value of foo is #{foo}\n\n"

That will drop into test.log and will put a couple line breaks on each side to make it easy for you to see it in the midst of all the other stuff there.

Hope that helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: