# Requiring records in the DB before testing

**URL:** <https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557>\
**Category:** Rails\
**Created:** [January 29, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557 "2010-01-29T21:19:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkohrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9a140/32.png) [@kkohrt](https://www.ruby-forum.com/u/kkohrt)\
**Post date:** [January 29, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557/1 "2010-01-29T21:19:07Z")

</div>

Is there any way I can require a small set of data be in the test  
database before any tests are run? (unit, functional, integration,  
cucumber, etc)  
Any way besides using fixtures, that is.  
Overload a rake task?  
Modify test\_helper?  
Any ideas?  
(Simple Case: I have to have a minimal set of user roles defined  
before any users can be created)

---

<div class="post-metadata">

**Author:** ![kkohrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9a140/32.png) [@kkohrt](https://www.ruby-forum.com/u/kkohrt)\
**Post date:** [January 29, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557/2 "2010-01-29T21:43:07Z")

</div>

Look at rake db:seed as one alternative.

---

<div class="post-metadata">

**Author:** ![kkohrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9a140/32.png) [@kkohrt](https://www.ruby-forum.com/u/kkohrt)\
**Post date:** [January 30, 2010, 12:10am UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557/3 "2010-01-30T00:10:01Z")

</div>

Steve R. wrote:

> On Jan 29, 2010, at 11:22 AM, kkohrt wrote:
> 
> > Is there any way I can require a small set of data be in the test  
> > database before any tests are run? (unit, functional, integration,  
> > cucumber, etc)  
> > Any way besides using fixtures, that is.  
> > Overload a rake task?  
> > Modify test\_helper?  
> > Any ideas?  
> > (Simple Case: I have to have a minimal set of user roles defined  
> > before any users can be created)
> 
> I use fixture replacements such as Fixjour or machinist. So,
> 
> 1.upto(10) do  
> Model.make # Machinist  
> end
> 
> That sort of thing. It’s good to put in a setup step.

Yes, factories are generally the best solution.

## Best,

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

---

<div class="post-metadata">

**Author:** ![kkohrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9a140/32.png) [@kkohrt](https://www.ruby-forum.com/u/kkohrt)\
**Post date:** [January 31, 2010, 3:45am UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557/4 "2010-01-31T03:45:32Z")

</div>

Hi  
Chek this

> **[GitHub - thoughtbot/factory\_bot: A library for setting up Ruby objects as...](https://github.com/thoughtbot/factory_bot)**
>
> A library for setting up Ruby objects as test data. - GitHub - thoughtbot/factory\_bot: A library for setting up Ruby objects as test data.

and its RDoc at  
[http://rdoc.info/projects/thoughtbot/factory\_girl](http://rdoc.info/projects/thoughtbot/factory_girl)

Sijo

---

<div class="post-metadata">

**Author:** ![kkohrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9a140/32.png) [@kkohrt](https://www.ruby-forum.com/u/kkohrt)\
**Post date:** [January 29, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/requiring-records-in-the-db-before-testing/182557/5 "2010-01-29T22:30:32Z")

</div>

On Jan 29, 2010, at 11:22 AM, kkohrt wrote:

> Is there any way I can require a small set of data be in the test  
> database before any tests are run? (unit, functional, integration,  
> cucumber, etc)  
> Any way besides using fixtures, that is.  
> Overload a rake task?  
> Modify test\_helper?  
> Any ideas?  
> (Simple Case: I have to have a minimal set of user roles defined  
> before any users can be created)

I use fixture replacements such as Fixjour or machinist. So,

1.upto(10) do  
Model.make # Machinist  
end

That sort of thing. It’s good to put in a setup step.
