[rspec-users] Examples not getting rolled back...

Tero Tilus tero at tilus.net
Thu Jan 8 04:56:12 EST 2009

Forgot to mention before.  I'm on Rails 2.2.2 and RSpec 1.1.4.

Inspired by older discussion touching this issue (see
http://www.nabble.com/Database-clearing-td19572270.html) I've now got

Spec::Runner.configure do |config|
  config.use_transactional_fixtures = false
  tables_to_truncate = 
    ActiveRecord::Base.connection.tables - ["schema_migrations"]
  config.before(:all) do
    tables_to_truncate.each do |table_name|
      ActiveRecord::Base.connection.execute("TRUNCATE TABLE

And if I run rake spec all specs pass.  But if I run script/spec
spec/models/class_foo_spec.rb then

  it "finds no ghost foos" do
    ClassFoo.should have(:no).records

fails.  That is the first example of describe ClassFoo.  Looks like
script/spec runs examples (see pastie http://pastie.org/354521) in the
order they are defined and rake spec in reverse order.  And that of
course makes ClassFoo.should have(:no).records fail when run _after_
examples that create ClassFoo instances (befause ive got transactions
off and the db state "bleeds" within one describe.

2009-01-07 16:11, Tero Tilus:
> I'm keep getting the following kind of pattern in my logs
>   ... log from example starts here ...
>     SQL (0.0ms)   BEGIN
>     SQL (0.0ms)   BEGIN
>     ClassFoo Create (0.2ms)   INSERT ...
>     Group Load (0.4ms)   SELECT ...
>     Contains Create (0.2ms)   INSERT ...
>   ... other stuff from example, no transaction stuff ...
>     SQL (1.9ms)   COMMIT
>     SQL (0.1ms)   ROLLBACK
>   ... log from example ends here ...

If I turn use_transactional_fixtures off, the outer transaction is

Tero Tilus ## 050 3635 235 ## http://tero.tilus.net/

More information about the rspec-users mailing list