Comparing Factory Girl with Fabrication
Asked Answered
L

4

25

There seems to be few resources about the fabrication gem, and I couldn't find a good comparison of the features that distinguish it from factory_girl. As of versions fabrication-2.2.3 and factory_girl-4.0.0, I can hardly find a difference. How do these gems compare? What can make fabrication a better choice and why? Have they both converged?

Loydloydie answered 29/8, 2012 at 13:29 Comment(2)
I guess you could choose an answer :)Pitchstone
@Pitchstone I wish, but all the answers are either subjective or incomplete. I wish someone could post an objective comparison.Loydloydie
Y
13

Just for what it's worth, I find fabrication to be faster, and I've experienced fewer errors with it. I think the big popularity gap is mostly because fabrication is much newer.

Yahweh answered 29/8, 2012 at 15:23 Comment(2)
Thanks @Andrew. Could you please elaborate on having fewer errors? What type of errors?Loydloydie
Basically with FG I ran into difficult to debug errors with associations etc. from time to time, and with Fabrication I had a lot less of that.Yahweh
D
11

We started out with Factory Girl, but wound up ripping it out in favour of constructing "actual" AR model instances; a decision I'm now revisiting.

So far, I like Fabricator. It seems to be easier to mock associations where absolutely nothing has to hit a real database, which never felt quite right in FG. YMMV, of course, and there's also:

Never theorise before you have data. Invariably, you end up twisting facts to suit theories, instead of theories to suit facts. — Holmes' Law of Factual Theories

Duumvir answered 9/1, 2013 at 2:53 Comment(2)
+1: When you want to avoid database access, Fabricator.build(:factory-name) just works. I have tried using both FactoryGirl.build and FactoryGirl.build_stubbed without success - it seems to get confused with model associations, and ends up hitting the database.Kloof
This topic is addressed here: github.com/thoughtbot/factory_girl/blob/master/…. Add the hook to ActiveSupportNotifications and you can see if your test is hitting the DB. You have to be careful how you structure your factories in order to get build_stubbed to avoid hitting the DB. It definitely works fine.Farrell
P
10

Factory Girl has an extra concept of trait which is really useful.

Other features seem more or less alike.

If you want to choose one, consider the number of downloads:

Could be useful when you look for help/support.

Pitchstone answered 29/8, 2012 at 13:38 Comment(3)
factory girl's doc lives here: github.com/thoughtbot/factory_girl/blob/master/…Pitchstone
Thanks. There is a clear difference in popularity (ruby-toolbox.com/categories/rails_fixture_replacement), but this doesn't necessarily mean that one is better than the other. It could be because one is older, or is more mainstream, or because the other has just matured recently.Loydloydie
The concept of traits is interesting (github.com/thoughtbot/factory_girl/blob/master/…), and apparently is not included in fabrication. Thanks.Loydloydie
M
4

I recently did a profiling of Fabricator vs FactoryGirl because I was curious about the performance comments mentioned by others. I also found Fabricator to be slightly faster (but only when using associations):

https://ksylvest.com/posts/2017-08-12/fabrication-vs-factorygirl

Given the runtimes were so close, I prefer Fabricator over FactoryGirl because setting up associations will by default propagate build vs create (instead of having to specify a strategy).

Mai answered 12/8, 2017 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.