# Test database and migrations

**URL:** <https://www.ruby-forum.com/t/test-database-and-migrations/197128>\
**Category:** Rails\
**Created:** [April 10, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128 "2010-04-10T16:01:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 10, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128/1 "2010-04-10T16:01:40Z")

</div>

Hi,

I’d like to have the results of a migration file that prepopulates my  
development database available for testing. So, right now, after the  
usual rake migration, I can access my prepopulated data via script/  
console, but not in my unit tests. I’ve reviewed comments on this  
subject but the custom rake tasks do not work or attempting a fix with  
RAILS\_ENV=test rake db:migrate as a command does not help either. Any  
suggestion on how I can get the results of my migration available in  
the test environment?

Thanks,

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 11, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128/2 "2010-04-11T22:47:54Z")

</div>

Grar,

My recommendation would be to not pre-populate with a migration. I  
would  
create a ruby script for that and have developers run that script to get  
setup. Then I would run that script in the test/test\_helper.rb setup or  
where ever is appropriate if I wanted that data available in my tests.

A more conventional approach would be to create fixtures with the test  
data. Then you can load those fixtures into your development database  
with  
“rake db:fixtures:load”.

Anthony C.

> **[Commonthread](https://commonthread.com)**
>
> Instantly connect and build rapport with anyone you meet, in person or online.

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 11, 2010, 11:56pm UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128/3 "2010-04-11T23:56:22Z")

</div>

Thanks guys.

What’s strange to me is the idea that Rails testing is not  
comprehensive, i.e., there are constants, e.g., zip codes, that you  
will use in production that you didn’t have access to in testing.  
While I appreciate that testing is built on cases and not exhaustive,  
I can think of situations in my developing app where I might like to  
check for “surprises” and I won’t want contrived cases derived from  
fixtures or factories. I guess what I’m getting at here is the idea  
that testing might involve statistical checks, e.g., what is the  
probability that I get some certain result? If I get many low  
probability results, maybe something’s wrong with my constants or the  
way I’m handling them, e.g., zip codes. Anyway, this concern straddles  
the line between uncertainty about inputs, e.g., app constant data  
like zip codes, and outputs – what an app does with them.

Thanks for your comments, I consider my question well answered.

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 12, 2010, 5:19am UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128/4 "2010-04-12T05:19:26Z")

</div>

You might be interested in the Faker gem that will create data for you  
in  
all kinds of formats.

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 15, 2010, 7:31am UTC](https://www.ruby-forum.com/t/test-database-and-migrations/197128/5 "2010-04-15T07:31:37Z")

</div>

Anthony C. wrote:

> Grar,
> 
> My recommendation would be to not pre-populate with a migration. I  
> would  
> create a ruby script for that and have developers run that script to get  
> setup. Then I would run that script in the test/test\_helper.rb setup or  
> where ever is appropriate if I wanted that data available in my tests.

Yes. In fact, Rails 2.3 has seeding built in, and for older versions,  
there’s seed-fu.

Seed data does not belong in migrations.

> A more conventional approach would be to create fixtures with the test  
> data. Then you can load those fixtures into your development database  
> with  
> “rake db:fixtures:load”.

No. Fixtures are a broken feature of Rails and should be avoided at all  
costs. Use factories instead.

> Anthony C.  
> [http://commonthread.com](http://commonthread.com)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
