How can a person toggle the use of transactional_f

2019-08-11 19:42发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-08-11 20:21

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.

查看更多
相关推荐>>
3楼-- · 2019-08-11 20:43

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
查看更多
登录 后发表回答