Friday, July 31, 2009

Make Faker work with Factory Girl

If you use the code below, faker will not churn out unique names

Factory.define :user do |u|
u.first_name Faker::Name.first_name
u.last_name Faker::Name.last_name
end

However putting curly braces around faker makes it work!

Factory.define :user do |u|
u.first_name { Faker::Name.first_name }
u.last_name { Faker::Name.last_name }
end

10 comments:

Unknown said...

save me hours, thanks!

Matt 01 said...

Thankss

arnodmental said...

Same for me, thanks a lot, I had tried :

Factory.define(:user) do |u|
u.firstname Proc.new { Faker::Name.first_name }
end


But did not return the actuel Proc result.

Carl said...

Thanks so much!

Gokul said...

thanks for sharing ...

Fitria Rahmaani said...

thanks a lot :D

Ranska said...

you're my hero !!!

Ranska said...

you're my hero !!!

Unknown said...

Thanks! However it's not quite clear to me what makes it work with the curly braces.... any chance someone can explain?

Jeremy Moritz said...

This still behaves the wrong way. how do i fix it? i need to assign the address to a variable to use more than once, but this is causing it to yield the same result each time i run `create(:store)`

factory :store do
fake_city_name = FFaker::Address.city

name { fake_city_name }
code { fake_city_name.upcase[0..2] }
end

Blog Archive