Forum: RSpec Database.truncate_all

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.
5dade05c7d95ee88f2d6d8970bb2071e?d=identicon&s=25 Jeff Talbot (Guest)
on 2008-12-06 06:28
(Received via mailing list)
There is a seemingly very useful line of code in the cucumber wiki
that I'd like to use:

  After do
    # Scenario teardown
    Database.truncate_all
  end

(from
http://github.com/aslakhellesoy/cucumber/wikis/mig...)

But when I run this I get an uninitialized constant error for
"Database".

Anybody know if/how I can make this call? Is there something I need to
require? Is there a different way of doing this now? I have a need to
not use transactional_fixtures for a specific suite of tests and I
want to clear out the database after every run so my tests are
isolated.

Thanks,
Jeff
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-12-06 07:35
(Received via mailing list)
On Fri, Dec 5, 2008 at 6:32 AM, Jeff Talbot <jeff.a.talbot@gmail.com>
wrote:

> There is a seemingly very useful line of code in the cucumber wiki
> that I'd like to use:
>
>  After do
>    # Scenario teardown
>    Database.truncate_all
>  end
>

That's just an example of some code you might call in an After. A little
lower, you see

TempFileManager.clean_up

which also looks like non-Rails/Cucumber code.

A simple way to achieve the former would be

class Database
  def self.truncate
    TheModel.destroy_all
    TheOtherModel.destroy_all
    # etc
 end
end

destroy_all isn't exactly the same as SQL TRUNCATE, but it might serve
your
purpose.

///ark
13ee02bb9012e843dbe7d0a23a6656f7?d=identicon&s=25 David Salgado (digitalronin)
on 2008-12-06 11:56
(Received via mailing list)
ISTR that "destroy_all" will instantiate each object, before
destroying it. If you just want to clean the database, I think
"delete_all" is the one you want (it just does a "delete from
[table]", if memory serves), and should be a lot quicker.

David


2008/12/6 Mark Wilden <mark@mwilden.com>:
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-12-06 18:15
(Received via mailing list)
On Sat, Dec 6, 2008 at 2:47 AM, David Salgado
<david@digitalronin.com>wrote:

> ISTR that "destroy_all" will instantiate each object, before
> destroying it. If you just want to clean the database, I think
> "delete_all" is the one you want (it just does a "delete from
> [table]", if memory serves), and should be a lot quicker.
>

Yes, you're right. I always get those mixed up.

///ark
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2008-12-06 18:30
(Received via mailing list)
Jeff Talbot wrote:
> But when I run this I get an uninitialized constant error for "Database".
>
> Anybody know if/how I can make this call? Is there something I need to
> require? Is there a different way of doing this now? I have a need to
> not use transactional_fixtures for a specific suite of tests and I
> want to clear out the database after every run so my tests are
> isolated.
>
> Thanks,
> Jeff
>

As pointed out this was just an example.  For ideas on how to implement
such a method you can read this previous thread about it:
http://www.mail-archive.com/rspec-users@rubyforge....

-Ben
5dade05c7d95ee88f2d6d8970bb2071e?d=identicon&s=25 Jeff Talbot (Guest)
on 2008-12-07 17:52
(Received via mailing list)
Cool. Thanks, guys.
95a6493586dcb150d4fb4aec8f81b766?d=identicon&s=25 Tero Tilus (Guest)
on 2008-12-07 19:28
(Received via mailing list)
2008-12-05 08:32, Jeff Talbot:
> I want to clear out the database after every run so my tests are
> isolated.

Maybe a minor detail, but I'd suggest you clear out the db _before_
each run.
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-12-07 20:07
(Received via mailing list)
On 7 Dec 2008, at 17:56, Tero Tilus wrote:

> 2008-12-05 08:32, Jeff Talbot:
>> I want to clear out the database after every run so my tests are
>> isolated.
>
> Maybe a minor detail, but I'd suggest you clear out the db _before_
> each run.

I respectfully disagree :)

If I need this done, I can run rake db:test:prepare

I think the tests should clean up after themselves.

As you can see from the thread Ben linked to, it's pretty easy to
watch what ActiveRecord is doing, and truncate all the tables that
have been touched during a scenario.


Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2008-12-07 20:21
(Received via mailing list)
On Sun, Dec 7, 2008 at 8:07 PM, Matt Wynne <matt@mattwynne.net> wrote:

>>
>
> I respectfully disagree :)
>

I respectfully disagree :-) Two reasons:

Firstly, this quickly leads to coupled tests. If test N+1 only passes if
test N cleans up, test N+1 will fail if run in isolation.
Secondly, if each test cleans up after itself, you can't manually look
at
the database to figure out why a test fails - there is no data!

Aslak
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-12-07 21:09
(Received via mailing list)
On 7 Dec 2008, at 19:20, aslak hellesoy wrote:

> each run.
>
> I respectfully disagree :)
>
> I respectfully disagree :-) Two reasons:
>
> Firstly, this quickly leads to coupled tests. If test N+1 only
> passes if test N cleans up, test N+1 will fail if run in isolation.

I think you misunderstand me. All our cucumber tests automatically
clean up the database after each scenario is run. No chance of nasty
coupling nonsense, but they do all assume that they'll start with an
empty database.

> Secondly, if each test cleans up after itself, you can't manually
> look at the database to figure out why a test fails - there is no
> data!

Good point - I never thought of that!

I just think a cleanup *before* a run would have to be more
indiscriminate, and therefor slower. Our strategy of keeping a list of
tables touched during a scenario then truncating them in the After do
block has worked very well for us. YMMV of course :)


>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
This topic is locked and can not be replied to.