Factory Girl vs. User.create -- what's the difference?
Asked Answered
D

1

7

This is an additional note to the question "Factory Girl - what's the purpose?"

I'm not sure whether my question is counted as a repetitive one, but I'm simply still not very clear after reading that post, since I think my doubt is still different.

Alright, so now that we can always use User.create() in before(:each) block in a Rspec test, why do we still bother using Factory Girl then?

This whole confuse occurred to me when I'm reading Michael Hartl's "Rails 3 tutorial", when he suddenly jumped into Factory Girl but used User.create() to build a User instance all the way through before that.

I wish someone can clarify this point for me, thx a lot!

Diaconicum answered 1/10, 2011 at 8:18 Comment(0)
P
4

You are correct but gems like factory girl and fabrication allow for significantly less code duplication across clases and within different tests when you need to test large sets of data that require different attribute values due to uniqueness requirements. It mixes in methods to easily "manufacture" many different objects for these types of tests.

Prerecord answered 1/10, 2011 at 16:32 Comment(3)
So the power of Factory Girls only kicks in when we need to build a large no. of instances and/or instances will be used in different test examples? Then as long as we need to build 1 or 2 instances only, using factory doesn't make much of a difference right?Diaconicum
@adam_0628, I agree with you conclusion. Also, maybe see the linked question for more explanation? #5184475Hurl
@ShaChris23, thanks for your comment. For the record, your link is exactly my "Factory Girl -- what's the purpose" one in my post. Haha thanks anyway!:)Diaconicum

© 2022 - 2024 — McMap. All rights reserved.