Factory Girl - what's the purpose?
Asked Answered
F

3

65

What's the purpose of Factory Girl in rspec tests when I could use before(:each) blocks? It feels like the only difference between Factory Girl and a before(:each) is that the factory prepares object creation outside of the test. Is this right?

Frunze answered 3/3, 2011 at 17:4 Comment(2)
This is how FactoryGirl interact with ActiveReccord cbabhusal.wordpress.com/2015/04/21/…Sperrylite
Answers here seem to be outdated and weird.Gluttonous
H
66

Gems like Factory Girl and Sham allow you to create templates for valid and re-usable objects. They were created in response to fixtures which where fixed records that had to be loaded into the database. They allow more customization when you instantiate the objects and they aim to ensure that you have a valid object to work with. They can be used anywhere in your tests and in your before and after test hooks.

before(:each), before(:all), after(:each) and after(:all) all aim to give you a place to do setup and teardown that will be shared amongst a test group. For example, if you are going to be creating a new valid user for every single test, then you'll want to do that in your before(:each) hook. If you are going to be clearing some files out of the filesystem, you want to do that in a before hook. If your tests all create a tmp file and you want to remove it after your test, you'll do that in your after(:each) or after(:all) hook.

The ways these two concepts differ is that Factories are not aimed at creating hooks around your tests, they are aimed at creating valid Ruby objects and records, so that you can keep your object creation flexible and DRY. Before and after hooks are aimed at setup and teardown tasks that are shared in an example group so that you can keep your setup and teardown code DRY.

Hagride answered 3/3, 2011 at 17:22 Comment(1)
Another way to say this: a FactoryGirl factory is like a give_me_a_valid_model function. If you're going to need that in more than one before :each block, it saves you some repetition. The more complicated it is to set up that valid model (associations, ensuring unique values, etc), the more effort it saves you.Anglosaxon
I
17

FactoryGirl replaces fixtures in tests. This way, you won't have to keep the fixtures up-to-date as you change the data model. Fixtures can also tend to get unwieldy as you add more edge cases.

FactoryGirl generates data on the fly and adding and removing fields is much easier. Also, you can use it in setup just how you'd use fixtures.

Does that make it clearer?

Ileana answered 3/3, 2011 at 17:18 Comment(2)
Took me awhile to figure out that factories are a "smart" replacement for fixtures. I'm still interested to learn more about the distinctions between mocks and stubs, too.Natatorium
It sure does, thank you. :) Good to know that they're a good replacement for fixtures. It was a little strange because the Hartl tutorials (which are good, btw) go in and out between fixtures and Factory girl. Which I found myself confusing. Now that I know, I'l probably use it more often. :)Frunze
O
6

also fixture definitions are global across your entire application. Factories can be local - so data specific to a isolated test case is in the setup for that particular context and not in a single global fixtures file.

This book covers the subject very well if you want some more reading matter

Otho answered 3/3, 2011 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.