Leftover fixture data causing errors

Maybe I’m not getting fixtures, but… I’m having problems as I start
using
fixtures in functional tests.

Data is left in my test database after running functional tests. Not the
data
created by the test-method (I’m using transactional fixtures), but data
from the
fixtures themselves. Fixture loading will delete & reload the data again
when
the test is re-run, but that doesn’t solve the problems.

Example: Say you have fixture data for tables customers, addresses, and
phones.
Addresses and phones both belong to customers. I run a functional test
that
loads fixtures for customers and addresses; it works great. However, the
fixture
data still remains in the database (Postgres 8.1, BTW) after the test
completes.

Now let’s say I run write a test for customer’s phones. It loads
fixtures for
phones and customers. Except that it will fail trying to delete the
existing
customers, because they still have dependent addresses tied to them in
the
database. End of test. Crash & burn.

I could add :addresses to the fixtures call in the phone functional
test. But 1)
addresses have nothing to do with phones except that they both belong to
customer, and 2) following this logic, I’ll soon have to load nearly
every
fixture in the system for most functional tests.

Am I doing something wrong here? This seems like such a glaringly
obvious goof
that my assumption is that I’m using fixtures wrong. What’s the proper
way to
use fixtures and insulate test cases from each other?

On May 9, 2006, at 4:19 PM, Kian wrote:

Now let’s say I run write a test for customer’s phones. It loads
fixtures for
phones and customers. Except that it will fail trying to delete the
existing
customers, because they still have dependent addresses tied to them
in the
database. End of test. Crash & burn.

We could have fixtures wipe data after each test run. However,
transactional fixtures were implemented to improve testing speed –
we’d be right back to full insert/delete per test case (though better
than per method, true.)

I could add :addresses to the fixtures call in the phone functional
test. But 1)
addresses have nothing to do with phones except that they both
belong to
customer, and 2) following this logic, I’ll soon have to load
nearly every
fixture in the system for most functional tests.

What if your pristine test data were available to all your tests
without making any fixtures declarations?

Am I doing something wrong here? This seems like such a glaringly
obvious goof
that my assumption is that I’m using fixtures wrong. What’s the
proper way to
use fixtures and insulate test cases from each other?

You aren’t. Fixtures remain “live” after testing and assume you’ll
manage your foreign keys appropriately. Experiment with declaring
them all on a common base class and removing them from your
individual test cases:

class MyApp::TestCase < Test::Unit::TestCase
fixtures :addresses, :phones, :customers, …
end

Or even rework your rake tasks to obviate fixtures altogether (e.g.
createdb -T fixtures test).

Having to declare fixtures at all, much less worrying about their
ordering and dependencies, is the real problem.

jeremy