[rspec-users] Verifying some understanding about manipulating DB data in before/after callbacks in RSpec

Stephen Eley sfeley at gmail.com
Thu Nov 6 17:03:10 EST 2008

On Thu, Nov 6, 2008 at 4:32 PM, Wes Gamble <weyus at att.net> wrote:
> Attempting to manipulate data directly in the DB in a before/after callback
> doesn't make sense since those changes will not persist across tests because
> of the transactionality implied by "self.use_transactional_fixtures = true".
>  Is that correct?

That's correct.  The philosophy of unit testing is that each test
should be independent, without any reliance on the tests run before or
after it.  Each test with the same setup and teardown should begin
with the database and environment in exactly the same state.  This is
important to RSpec because you can't guarantee the order in which
tests or spec files might run.  So if you're relying on state changes,
they won't necessarily work the way you want them to.

Anyway, what the heck are you writing where it's important that data
be reset "in a certain way?"  If that's crucial to your code, don't
test it by relying on after(:each) -- just write an example where you
destroy things in whatever order and then verify the results.  I might
also suggest, however, that you consider whether it's really a good
idea to have it crucial to your code.

Have Fun,
   Steve Eley (sfeley at gmail.com)
   ESCAPE POD - The Science Fiction Podcast Magazine

More information about the rspec-users mailing list