Forum: RSpec Examples not getting rolled back...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tero T. (Guest)
on 2009-01-07 16:14
(Received via mailing list)
I assume there's now something I'm totally missing here.

I'm creating stuff in examples, just plain Foo.create, and the results
aren't getting rolled back.  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 ...

all examples of the following form produce it

  describe ClassFoo do
    ...
    it "plim-ploms" do
      foo = ClassFoo.create
      ...
    end
    ...
  end

I'm just wondering if this is caused by those nested transactions.
MySQL (which I happen to use) doesn't support nested transactions and
is documented to just silently commit open transaction on new BEGIN.
Taken that I'd expect last ROLLBACK not to have any effect.

Where do those transactions come from?  I'd guess outer transaction
to be from RSpec and inner from ActiveRecord (oh, I'm on Rails).

And of course, what can I do about this...

A bit more stuff in pastie http://pastie.org/354521
Stephen E. (Guest)
on 2009-01-07 20:20
(Received via mailing list)
On Wed, Jan 7, 2009 at 9:11 AM, Tero T. <removed_email_address@domain.invalid> 
wrote:
>
> I'm creating stuff in examples, just plain Foo.create, and the results
> aren't getting rolled back.  I'm keep getting the following kind of
> pattern in my logs

You probably thought about this already, but did you check the setting
of config.use_transactional_fixtures in config.spec_helper.rb?





--
Have Fun,
   Steve E. (removed_email_address@domain.invalid)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
Tero T. (Guest)
on 2009-01-07 20:53
(Received via mailing list)
2009-01-07 13:04, Stephen E.:
> config.use_transactional_fixtures in config.spec_helper.rb?

Tried true, false and commenting out.  I could not see any difference.
David C. (Guest)
on 2009-01-07 22:02
(Received via mailing list)
On Wed, Jan 7, 2009 at 8:11 AM, Tero T. <removed_email_address@domain.invalid> 
wrote:
>    Group Load (0.4ms)   SELECT ...
>    it "plim-ploms" do
>
> Where do those transactions come from?  I'd guess outer transaction
> to be from RSpec and inner from ActiveRecord (oh, I'm on Rails).

Is the app code opening transactions?
Tero T. (Guest)
on 2009-01-07 23:25
(Received via mailing list)
2009-01-07 13:08, David C.:
> Is the app code opening transactions?

Yes, but only one spot (iirc) which is not anywhere near the model
whose test is failing here.  I'll verify tomorrow that the failing
test really doesn't run the app code in question.
Tero T. (Guest)
on 2009-01-08 12:02
(Received via mailing list)
2009-01-07 23:23, Tero T.:
> 2009-01-07 13:08, David C.:
> > Is the app code opening transactions?
>
> Yes, but only one spot (iirc) which is not anywhere near the model
> whose test is failing here.  I'll verify tomorrow that the failing
> test really doesn't run the app code in question.

Verified, and it didn't.  Transactions I see on the log when running
the spec in question come from ActiveRecord (and RSpec if I have
use_transactional_fixtures = true).
Tero T. (Guest)
on 2009-01-08 12:27
(Received via mailing list)
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
      #{table_name};")
    end
  end
  ...
end

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
  end

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 T.:
>     SQL (0.1ms)   ROLLBACK
>   ... log from example ends here ...

If I turn use_transactional_fixtures off, the outer transaction is
gone.
David C. (Guest)
on 2009-01-08 15:48
(Received via mailing list)
On Thu, Jan 8, 2009 at 3:56 AM, Tero T. <removed_email_address@domain.invalid> 
wrote:
> Forgot to mention before.  I'm on Rails 2.2.2 and RSpec 1.1.4.

Aha! There's the problem. RSpec-1.1.4 was released in May and Rails
2.2.2 was released AFTER in November.

I'd grab the 1.1.12 release candidates from github:

gem sources -a http://gems.github.com
[sudo] gem install dchelimsky-rspec
[sudo] gem install dchelimsky-rspec-rails

Cheers,
David
Tero T. (Guest)
on 2009-01-09 16:11
(Received via mailing list)
2009-01-08 07:37, David C.:
> I'd grab the 1.1.12 release candidates from github:
>
> gem sources -a http://gems.github.com
> [sudo] gem install dchelimsky-rspec
> [sudo] gem install dchelimsky-rspec-rails

It gives me 1.1.11.6.  Is that a "1.1.12 release candidate"?
Could I git clone 1.1.12 somewhere?  I'd love to do that.
David C. (Guest)
on 2009-01-09 16:16
(Received via mailing list)
On Fri, Jan 9, 2009 at 6:37 AM, Tero T. <removed_email_address@domain.invalid> 
wrote:
> 2009-01-08 07:37, David C.:
>> I'd grab the 1.1.12 release candidates from github:
>>
>> gem sources -a http://gems.github.com
>> [sudo] gem install dchelimsky-rspec
>> [sudo] gem install dchelimsky-rspec-rails
>
> It gives me 1.1.11.6.  Is that a "1.1.12 release candidate"?
> Could I git clone 1.1.12 somewhere?  I'd love to do that.

Sure. Take a look at http://github.com/dchelimsky/rspec/wikis
This topic is locked and can not be replied to.