# Seeds.rb and rake test tasks

**URL:** <https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290>\
**Category:** Rails\
**Created:** [September 28, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290 "2009-09-28T17:37:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/1 "2009-09-28T17:37:46Z")

</div>

We’ve started using seeds.rb to populate some low-level required data  
into our database, but we’re having some issues with this data getting  
removed and not re-entered while running the tests.

Is seeding part of the testing lifecycle or do we have to replicate  
the seed data in the fixtures as well?

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/2 "2009-09-28T18:36:19Z")

</div>

My guess is that you’d need to add this to your test setup. By  
default, everything is rolled back from your test database after every  
test.

def setup  
`rake db:seed`  
end

With backticks it will run the shell process, and should wait for it  
to finish before proceeding.

Nothing comes up when I search the api docs for ‘seed’, so for now it  
may just be in rake. It may take some digging in the source to find  
out whether you can seed the database directly from Rails. Or there  
may be a gem that can help.

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/3 "2009-09-28T18:59:09Z")

</div>

I’m a bit new to the Rails stuff, but what’s the expected convention  
for running the tests.

I’ve been doing -  
rake db:setup RAILS\_ENV=test  
rake test:unit RAILS\_ENV=test

Just to make sure I’m not fighting the conventions.

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/4 "2009-09-28T19:56:32Z")

</div>

If you run

rake -T test

in your project directory, you can see all your available test tasks.  
Two helpful ones are:

rake db:test:clone  
rake db:test:load

Oh, another option for your original problem is to turn off db  
rollbacks in your test environment. Unfortunately I can’t remember the  
option for doing that. Just keep in mind that you’ll want to manually  
clear out the database occasionally or you may see your tests slow  
down.

> I’d recommend installing Autotest. (Also give serious thought to  
> RSpec.)

Seriously. This is super helpful. Also, you may want to look into  
something like factory\_girl. If your tests aren’t really coupled to  
your seed data, they just need dummy records for your models, this may  
be a better way to go. That way your tests don’t assume particular  
records in your database.

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/5 "2009-09-28T19:19:35Z")

</div>

Nathan B. wrote:

> I’m a bit new to the Rails stuff, but what’s the expected convention  
> for running the tests.
> 
> I’ve been doing -  
> rake db:setup RAILS\_ENV=test  
> rake test:unit RAILS\_ENV=test
> 
> Just to make sure I’m not fighting the conventions.

I’d recommend installing Autotest. (Also give serious thought to  
RSpec.)

## Best,

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

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [September 28, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/seeds-rb-and-rake-test-tasks/176290/6 "2009-09-28T20:14:40Z")

</div>

On Sep 28, 12:53 pm, sax [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Oh, another option for your original problem is to turn off db  
> your seed data, they just need dummy records for your models, this may  
> be a better way to go. That way your tests don’t assume particular  
> records in your database.

Unfortunately, it’s not the tests that depend upon the data, it’s the  
code itself. The seed data is reference data, which is basically just  
one step above an enumeration.
