Dr. Chocolate
Dr. Chocolate

Reputation: 2165

Does factory girl call the model?

If I just need a basic model is there any difference in creating a model directly vs creating a model with factory girl?

I.E.

User.new(..all the correct params)

vs

Factory.create :user

My ultimate question is:

When testing controller does creating a model directly make the test less reliable?

Upvotes: 1

Views: 94

Answers (2)

forthowin
forthowin

Reputation: 387

Creating a new object through Factory Girl makes the test more reliable by drying up your code. For example, if you decide later to add a new attribute to the users table, you only have 1 place to update that attribute. If you were to use User.new(....), you would have to go and hunt down all of those in your test files to update the change.

Upvotes: 0

Piotr Brudny
Piotr Brudny

Reputation: 656

There is no big difference. You can create a new object directly. However factory girl can deal with associations and you usually put there some default data so it is just easier for you.

It is also easier to re-use the code. Otherwise whenever you create user you have to set these params.

That is why it is kind of good practise to use factory-girl

Upvotes: 2

Related Questions