How can a person toggle the use of transactional_fixtures for one set of tests using Test::Unit?

半世苍凉 提交于 2019-12-11 11:16:24

问题


I have some thinking-sphinx tests that I need to turn off transactions for to prevent mysql locks, but in doing so I break a lot of other tests previously written, so I need to be able to toggle the setting.

I have found similar questions regarding rspec, but none for Test::Unit.

I have tried self.use_transactional_fixtures = false which is what allows the sphinx tests to pass, but causes others to break. I am sure I could set that to true in all the other tests, but that would also require all other tests to include the code snippet as well which is messy.

I have also tried uses_transaction :test_method_name, but that works the same as the previously mentioned method.

Thanks in advance for any help.


回答1:


You should be able to set use_transactional_fixtures to false for just the sphinx related tests that you have, but leave it true for the other tests you have. The key will be to split your tests into separate test classes. So in test_helper.rb you'll still have:

self.use_transactional_fixtures = true

But then for your other tests you'll do something like

class PostTest < ActiveSupport::TestCase
  # your normal post tests that require transactional fixtures
end

class SphinxRelatedPostTest < ActiveSupport::TestCase
  self.use_transactional_fixtures = false
  # your other tests
end

That should mean that your regular tests run using the fast transactional fixtures, but rails will use the DELETE / INSERT method as required.




回答2:


You can also use uses_transaction to disable them for specific method/test.

class MyTest < ActiveSupport::TestCase

  uses_transaction :test_something
  test "something" do
    "test something like callback"
  end

end


来源:https://stackoverflow.com/questions/4055147/how-can-a-person-toggle-the-use-of-transactional-fixtures-for-one-set-of-tests-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!