# Having a base date set for RSpec Testing?

**URL:** <https://www.ruby-forum.com/t/having-a-base-date-set-for-rspec-testing/138330>\
**Category:** Rails\
**Created:** [May 22, 2008, 4:06am UTC](https://www.ruby-forum.com/t/having-a-base-date-set-for-rspec-testing/138330 "2008-05-22T04:06:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 22, 2008, 4:06am UTC](https://www.ruby-forum.com/t/having-a-base-date-set-for-rspec-testing/138330/1 "2008-05-22T04:06:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [July 13, 2008, 12:15am UTC](https://www.ruby-forum.com/t/having-a-base-date-set-for-rspec-testing/138330/2 "2008-07-13T00:15:55Z")

</div>

On Wed, May 21, 2008 at 9:05 PM, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://github.com/dchelimsky/rspec-rails/wikis)) for this to work  
consistently.

HTH,  
David
