# My web app seed data and cucumber

**URL:** <https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287>\
**Category:** Rails\
**Created:** [July 8, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287 "2010-07-08T20:48:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 8, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287/1 "2010-07-08T20:48:56Z")

</div>

I’m writing a web app which is used a SaS. Each customer has their own  
db and app directory. I have a rake task which creates all necessary  
minimum data to run their website: default rights and roles, a  
superadmin user, a “us\_states” table already populated, some local  
depots and terminals (it’s a logistics app).

I don’t have any cucumber scenarios for it and I just started building  
some. I’m a beginner with cucumber.

I first put that task in a Given line, but that is pretty much is a  
given for all scenarios, and it doesn’t make much sense to non-  
programmers who look at the scenarios (for humans, it such a given  
that it doesn’t need to be expressed consciously) so I moved it into  
hooks.rb.

My first scenario looks like this:

1 Feature: Place an order  
2 In order to keep orders in the database  
3 As a admin  
4 I want to place orders  
5  
6 Scenario: Using common legs  
7 Given I have 1 customers  
8 And I’m on the homepage  
9 And I follow “Place an Order”  
10 When I select the customer  
11 And I select the SSLine  
12 And I click “Use Common Legs”  
13 Then I should see “PICKUP AT”  
14 And I should see “DELIVER TO” or “LOAD AT”  
15 And I should see EMPTY RETURN

1 Before do  
2 MinimumData.new(‘costi’, ‘1234’).populate  
3 end

My hooks.rb looks like this:  
1 Before do  
2 MinimumData.new(‘costi’, ‘1234’).populate #username and  
password  
3 end

I have three questions:

1. I don’t want to run this MinimumData.populate task before each  
scenario because it takes 8 seconds. Should I make it run just once,  
globally?
2. Do I have to cleanup the database with an After.do? I really don’t  
want to do that, because I will duplicate the logic in the After.do,  
only with Model.delete\_all statements. I noticed that after my first  
run, the test db has all that data still in. I can purge it with rake  
db:test:purge and the reinitialize it. Is that a good practice?
3. Have you got any other comments or advice by looking at the code?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 8, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287/2 "2010-07-08T21:34:43Z")

</div>

Constantin G. wrote:

> I have three questions:
> 
> 1. I don’t want to run this MinimumData.populate task before each  
> scenario because it takes 8 seconds. Should I make it run just once,  
> globally?

Don’t use seed data for running tests. I would highly recommend using a  
Factory framework instead. Factories allow you to create only the data  
actually used by individual tests. In many cases factory objects can be  
use in memory only and never have to touch the database. Also mocking  
can be used to greatly reduce the overhead you’re seeing with your seed  
data generation technique.

> **[GitHub - notahat/machinist: Fixtures aren't fun. Machinist is.](https://github.com/notahat/machinist)**
>
> Fixtures aren't fun. Machinist is. Contribute to notahat/machinist development by creating an account on GitHub.

> **[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.

> 1. Do I have to cleanup the database with an After.do? I really don’t  
> want to do that, because I will duplicate the logic in the After.do,  
> only with Model.delete\_all statements. I noticed that after my first  
> run, the test db has all that data still in. I can purge it with rake  
> db:test:purge and the reinitialize it. Is that a good practice?

The test database should be reset after every test. You don’t want to  
have to deal with side-effects that cross test cases. This is why  
fixture data, or even better, Factory frameworks exist.

> 1. Have you got any other comments or advice by looking at the code?

Part of the reason your data generation takes so long is that you’re  
probably doing it all with Ruby and ActiveRecord. This it completely  
fine for a one-time population of seed data. Actually Rails provides a  
standard way to do that. See rake db:seed for more details on this, in  
case you were unaware.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 8, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287/3 "2010-07-08T21:48:40Z")

</div>

On 8 juil, 14:34, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> can be used to greatly reduce the overhead you’re seeing with your seed  
> data generation technique.
> 
> [http://github.com/notahat/machinisthttp://github.com/thoughtbot/factory\_girl](http://github.com/notahat/machinisthttp://github.com/thoughtbot/factory_girl)

I was under the impression the cucumber tests have to test the real  
app and have to go through the real database and frankly I would like  
that too.

> > 1. Do I have to cleanup the database with an After.do? I really don’t  
> > want to do that, because I will duplicate the logic in the After.do,  
> > only with Model.delete\_all statements. I noticed that after my first  
> > run, the test db has all that data still in. I can purge it with rake  
> > db:test:purge and the reinitialize it. Is that a good practice?
> 
> The test database should be reset after every test. You don’t want to  
> have to deal with side-effects that cross test cases. This is why  
> fixture data, or even better, Factory frameworks exist.

How do I reset the data after every test? Should have been done  
automatically by cucumber?

> > 1. Have you got any other comments or advice by looking at the code?
> 
> Part of the reason your data generation takes so long is that you’re  
> probably doing it all with Ruby and ActiveRecord. This it completely  
> fine for a one-time population of seed data. Actually Rails provides a  
> standard way to do that. See rake db:seed for more details on this, in  
> case you were unaware.

It is a one time operation. So how should I make it run only once for  
all cucumber tests and then who will do the cleanup after each  
scenario?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 8, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/my-web-app-seed-data-and-cucumber/191287/4 "2010-07-08T22:29:52Z")

</div>

Constantin G. wrote:

> On 8 juil, 14:34, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > can be used to greatly reduce the overhead you’re seeing with your seed  
> > data generation technique.
> > 
> > [http://github.com/notahat/machinisthttp://github.com/thoughtbot/factory\_girl](http://github.com/notahat/machinisthttp://github.com/thoughtbot/factory_girl)
> 
> I was under the impression the cucumber tests have to test the real  
> app and have to go through the real database and frankly I would like  
> that too.

Not sure what gave you that impression. I would never test with “real”  
data. Cucumber is really designed to replace integration tests. All  
testing should use either fixture data or factories.

Your wanting to use seed data for your tests goes against sound testing  
principals, no matter how you feel about it. Ultimately it’s your  
project, do what you want. I’m just trying to lead you toward correct  
thinking.

> > > 1. Do I have to cleanup the database with an After.do? I really don’t  
> > > want to do that, because I will duplicate the logic in the After.do,  
> > > only with Model.delete\_all statements. I noticed that after my first  
> > > run, the test db has all that data still in. I can purge it with rake  
> > > db:test:purge and the reinitialize it. Is that a good practice?
> > 
> > The test database should be reset after every test. You don’t want to  
> > have to deal with side-effects that cross test cases. This is why  
> > fixture data, or even better, Factory frameworks exist.
> 
> How do I reset the data after every test? Should have been done  
> automatically by cucumber?

Exactly, which is why Cucumber resets your database between tests. I  
didn’t say that you would have to reset it.

> > > 1. Have you got any other comments or advice by looking at the code?
> > 
> > Part of the reason your data generation takes so long is that you’re  
> > probably doing it all with Ruby and ActiveRecord. This it completely  
> > fine for a one-time population of seed data. Actually Rails provides a  
> > standard way to do that. See rake db:seed for more details on this, in  
> > case you were unaware.
> 
> It is a one time operation. So how should I make it run only once for  
> all cucumber tests and then who will do the cleanup after each  
> scenario?

By “one time” I mean once after creating your production (or  
development) database. Possibly more that once for the development  
database as you migrate, reset and reload your development database.
