Forum: Ruby on Rails Rspec - want to NOT clear database between tests

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
75b83ed31a2c53622d274c85c701f18b?d=identicon&s=25 Carilda Thomas (carilda)
on 2012-06-30 02:55
Rails 3.2.1 Ruby 1.9.3p125 Rspec 2.8.0
I have a utility written in ruby that executes within a Rails
environment. It creates database records in one table based on the info
in other tables. A date and a name determine uniqueness of these created

To test this, I have to run the utility giving it date parameters for a
year + 4 days and then, for each test, test to see if each of 34 records
has or has not been created according to a spreadsheet that contains the
expected result.

The utility creates records according to a number of different
algorithms so that, for example, on the 21st of the month it may create
a record for the 19th and another for the 31st, so it's not feasible to
run this on a day-by-day basis.

The thing is, I don't want to have to run the utility inside every test
- it takes several minutes for each run and there will be 34 x 366 tests
(plus a few extras).

So, I run the utility and then, when I execute the first test, I
discover that the records created by the utility are gone and the
database contains just the fixtures data.

Roughly, I have:

describe "comprehensive test" do
  context "all year" do
    <utility to extract infrastructure data from dev database>
    <code to symlink a yaml file with one or another mutually exclusive
seed records>
    fixtures :all
    <execute utility for 2012-01-01 to 2013-01-05>
    it "should have more than the seed records in the table examined" do
      Table.all.count.should be > 2   # number of seed records
    <each day, each identifier, try to retrieve record and check against
0 or 1 on the corresponding spreadsheet entry>
  context "already existing records" do
    <empty database>
    <code to symlink the other yaml file into a fixture>
    fixtures :all
    <execute utility for 2012-05-31 only>
    <test that a specific 6 records have not been created>

One test actually implemented and it fails.

The one test will be followed by complex looping code reading the
spreadsheet and requesting the corresponding record and either it should
be there or it shouldn't. That's over 12,000 tests because not creating
a record when it shouldn't be there is as important as creating it when
it should. And then there's the second context...

Running a single month is not sufficient because holidays, days of
the week, first and last days of the month figure in for different
record creation algorithms.

But I can't get past this one test. How do I stop rspec from clearing
the relevant table before the test? I watch the records I want being
created, but then the db is restored to just the fixtures.

Note that I could have done this in shell in about 1/2 day but I've been
ordered to do this in the rspec framework. I feel like I'm building a
blivet here (10 pounds of ... slop in a 5-lb bag).

Please someone help? Otherwise I will just have to write the shell
script and call it from rspec :(
7e6cc29aaa8366e72eb705bbc1f2fc92?d=identicon&s=25 Martin Streicher (Guest)
on 2012-06-30 14:50
(Received via mailing list)
before(:all) seems well-suited to this problem. It creates data that
persists across transactions.
75b83ed31a2c53622d274c85c701f18b?d=identicon&s=25 Carilda Thomas (carilda)
on 2012-07-01 21:09
Martin Streicher wrote in post #1066742:
> before(:all) seems well-suited to this problem. It creates data that
> persists across transactions.

Thank you but (after much googling) I discovered that one cannot load
fixtures in before(:all).

At this point, I'm really about to give up on rspec and just do it in
plain ruby.
This topic is locked and can not be replied to.