Factories/Fixtures vs simple Model.create(...)?
Asked Answered
M

3

10

What's the purpose of factories/fixtures (I know factories act like fixtures, but a bit clearer) when you can simply use ActiveRecord in your test to create the database entry? i.e. News.create(…)

I just don't see any advantage of using Factory Girl instead of simply create a new let say a new User using ActiveRecord methods..

Thanks

Maisonette answered 14/4, 2012 at 16:17 Comment(0)
W
5

Having Factories and following the Test Data Building pattern will make you do a little bit of work up front, but will really save you time and work in the future.

Let's say you have a Car model, and that Car has an Owner, and that Owner needs an Address. Also each of those have other fields. If you want to follow the approach of using the Models directly, you will have to create those objects (and the correspondent relations) in each step definition that requires them. With Factory? You will define it just once.

Once you have defined the Factory in a single place with its correspondent structure, all you will have to do is ask for a Car and Factory will take car of all the dependencies with other models. Isn't that cool? At the end you want to focus on testing at this point.

What it's also really cool is that you can overwrite particular attributes, so you might have for example something like this, if you want to overwrite the attribute speed:

Given /^I have a car running^/
 Factory :car, speed => 100
end

From my humble point of view, I love Factory Girl because it makes my test code easy to mantain and really easy to read.

Wellstacked answered 14/4, 2012 at 17:5 Comment(2)
Counterpoints: All of these arguments apply to fixtures as well. Fixtures are faster overall. Any other reasons?Johnny
@Johnny Despite I use and I like fixtures, I sometime find them hard to maintain.Analogous
M
6

Factories

Creates/builds an object and then you use it only when called upon in a test.

You define the "happy path" of the object, including associations, in one file (as ryan mentioned) and then just maintain one file for when your schema/model associations change.

Fixtures

Mimics what the database should look like for a single record, it's loaded into the test database and then used.

Fixtures are end state of the model and live in the database, so are loaded once, and can be used in the tests as you see fit.

Multiple files and you lay out your fixture files so that each file and association are taken care of.

NewModel.create(...)

Is created once and used once in one test or in one before(:each) block

If you have 100 of these blocks, that is writing and maintaining 100 different Objects, let alone the Associations like @user.profile.create(....) 100 times.

Advantages of Factories over NewModel.create

One file to maintain, rather than grepping and replacing multiple build or create in your lines of code.

Note It must be said that in my test suite I want it to be quick as possible, so i am dropping FactoryGirl and using Fixtures and NewModel.create moving forward. Just to see if it speeds it up. I'm working on the theory that FG is slowing my suite down, as is let() and before(:each)

Monia answered 22/12, 2012 at 12:34 Comment(0)
W
5

Having Factories and following the Test Data Building pattern will make you do a little bit of work up front, but will really save you time and work in the future.

Let's say you have a Car model, and that Car has an Owner, and that Owner needs an Address. Also each of those have other fields. If you want to follow the approach of using the Models directly, you will have to create those objects (and the correspondent relations) in each step definition that requires them. With Factory? You will define it just once.

Once you have defined the Factory in a single place with its correspondent structure, all you will have to do is ask for a Car and Factory will take car of all the dependencies with other models. Isn't that cool? At the end you want to focus on testing at this point.

What it's also really cool is that you can overwrite particular attributes, so you might have for example something like this, if you want to overwrite the attribute speed:

Given /^I have a car running^/
 Factory :car, speed => 100
end

From my humble point of view, I love Factory Girl because it makes my test code easy to mantain and really easy to read.

Wellstacked answered 14/4, 2012 at 17:5 Comment(2)
Counterpoints: All of these arguments apply to fixtures as well. Fixtures are faster overall. Any other reasons?Johnny
@Johnny Despite I use and I like fixtures, I sometime find them hard to maintain.Analogous
W
2

Factories allow you to put test setup in one file. For instance, my project factory is in spec/support/factories/project_factory.rb.

If I want to change the default title or perhaps add another attribute, I do that in one file. With AR splattered everywhere throughout my tests, I would need to change that in every single instance of that use.

Wilkins answered 14/4, 2012 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.