13

I am in the process of upgrading my application to Rails 3. I started using Rspec 2 with Rails 3. I need to turn off transactional fixtures for some of my rspec tests. Prior I used the following code in my model specs

 before(:all) do
    ActiveSupport::TestCase.use_transactional_fixtures = false
  end

  after(:all) do
    ActiveSupport::TestCase.use_transactional_fixtures = true
    clean_engine_database
  end

That now gives me the error:

 Failure/Error: ActiveSupport::TestCase.use_transactional_fixtures = false
     undefined method `use_transactional_fixtures=' for ActiveSupport::TestCase:Class

Is there a way to do this per test block in Rails 3 with Rspec 2?

shingara
  • 46,608
  • 11
  • 99
  • 105
Nicolo77
  • 1,825
  • 4
  • 24
  • 34
  • 1
    possible duplicate of [Turn off transactional fixtures for one spec with RSpec 2](http://stackoverflow.com/questions/3853098/turn-off-transactional-fixtures-for-one-spec-with-rspec-2) – shingara Oct 11 '10 at 16:10
  • 1
    this doesn't seem to be duplicate. they are just related. – Lailson Bandeira Oct 13 '10 at 00:52

3 Answers3

23

I'm looking for the answer to this question, came across this blog entry

It suggests to declare inside the describe block

describe "xxx" do
  self.use_transactional_fixtures = false
  ...

I tried it with Rails 3.0.7 with RSpec 2.6.3, and looks like working.

shigeya
  • 4,862
  • 3
  • 32
  • 33
  • 3
    Nice. I found that you can also put it at the beginning of a `context` block, and it will only apply to tests inside it. – Kelvin Jul 10 '12 at 17:44
  • 1
    Works just fine (and the same way) with rails 3.2.2 and mini-test 2.5.1 – eclectic923 Sep 06 '12 at 20:40
  • 3
    Update: Using this now gives the warning `DEPRECATION WARNING: use_transactional_fixtures= is deprecated and will be removed from Rails 5.1 (use use_transactional_tests= instead)`. However, `self.use_transactional_tests = false` works just fine. – Amin Shah Gilani Sep 13 '16 at 18:35
  • Oops. that's bad. Then, I have to find out workaround when I upgrade to rails 5... thanks. – shigeya Sep 29 '16 at 23:07
0

You can disable transactional fixtures globally by putting config.use_transactional_fixtures = false on the spec_helper.rb. If you want to control them by test (e.g. use transactional just on some of them), you can set this behavior with DatabaseCleaner.

I've had a related problem when testing pages with javascript on the browser (a scenario that does not work with transactional fixtures). Here's how I managed to work around it: http://github.com/lailsonbm/contact_manager_app

Lailson Bandeira
  • 754
  • 1
  • 7
  • 18
0
RSpec.configure do |config|
  config.use_transactional_fixtures = true
end
David Chelimsky
  • 8,920
  • 2
  • 38
  • 30
  • This didn't work. I put this in the before(:all) and it doesn't seem to work. I'm still getting this error which is typically solved by turning off transactional fixtures for the test. PGError: ERROR: SET TRANSACTION ISOLATION LEVEL must be called before any query – Nicolo77 Oct 13 '10 at 16:51
  • 2
    You should put this code outside any `:all` or `describe` block. The spec_helper.rb file is a good place if you wanna set this globally. And I think David meant to say `false` instead of `true`. – Lailson Bandeira Oct 14 '10 at 02:11