[rspec-users] Database.truncate_all

Matt Wynne matt at mattwynne.net
Sun Dec 7 15:09:33 EST 2008


On 7 Dec 2008, at 19:20, aslak hellesoy wrote:

>
>
> On Sun, Dec 7, 2008 at 8:07 PM, Matt Wynne <matt at mattwynne.net> wrote:
> 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 :)
>
> 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 :)


> Aslak
>
>
> 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
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com



More information about the rspec-users mailing list