Having a base date set for RSpec Testing?

I have a quick questions.

I’m developing an application with has about 15-20 tables in the DB,
with static data that is necessary for the system to operate
(everything from US States to various enumerated values).

The obvious problem is that RSpec wipes your database for each
example, and needed to put a call to 15-20 fixtures on each describe
block seems excessive.

And in my case it really can’t be mocked/stubbed out. The values are
used during the parsing of the class file:

named_scope :open, :conditions => [“lead_status_id IN (?)”, %w{New
Incubating Client UAG}.collect{|x| LeadStatus[x].id}]

which means that I will need to add the LeadStatus fixtures to every
test that touches the lead model.

Is there anyway to specify that certain tables don’t get wiped during
the setup for each example?

thanks

On Wed, May 21, 2008 at 9:05 PM, Andrew S. [email protected] wrote:

I have a quick questions.

I’m developing an application with has about 15-20 tables in the DB,
with static data that is necessary for the system to operate
(everything from US States to various enumerated values).

The obvious problem is that RSpec wipes your database for each
example, and needed to put a call to 15-20 fixtures on each describe
block seems excessive.

This is not exactly what happens. RSpec configures Rails testing code
to run with transactional fixtures by default (look for
config.use_transactional_fixtures in spec/spec_helper.rb).

This declaration, use_transactional_fixtures, is a bit of a misnomer.
What it means really is "run each example in a transaction, and roll
that transaction back. So it doesn’t wipe out the database between
each example - it just restores it to the state it was in before the
example.

This means that you can set up global data before any examples get
run, and there are a couple of ways you can accomplish this. The
simplest and cleanest way (IMO) is to add a before(:suite) block to
the config in spec/spec_helper.rb:

config.before(:suite) do

set up all the global data

end

This will run after db:test:prepare and before any examples. Then
after each example the db will be restored to the state created in the
before(:suite) block.

NOTE that the before(:suite) functionality was a bit broken until
about 10 minutes ago, so you need the latest rspec from github
(Home · dchelimsky/rspec-rails Wiki · GitHub) for this to work
consistently.

HTH,
David