I'm new to using factory_girl and I'm having a little trouble
understanding how associations should work.
My factory.rb looks like this:
Factory.define :user do |u|
u.name 'John Smith'
u.administrator false
u.email_address 'john....@example.com'
u.password 'hello'
end
Factory.define :location do |loc|
loc.name "Canary Wharf"
loc.latitude 123
loc.longitude 456
loc.postal_code 'SE10 0RF'
loc.association :user
end
Factory.define :app do |a|
a.name "My App"
a.app_store_url "http://blah"
a.association :user
a.association :app_category
end
And my model spec looks like this:
describe Widget do
it "..." do
Factory.create(:app)
Factory.create(:location)
end
end
When I run this, I get the following error:
Validation failed: Email address has already been taken
My user model requires a unique email address so it seems that two
separate user records are being created. How can I tell factory_girl
to use the same user for both associations?
Andy
Two things.
First, FG has sequences to help with the dup email addresses:
Factory.sequence :email do |n|
"user#{n}@example.com"
end
Factory.define :user do |user|
user.email { Factory.next(:email) }
user.password { "password" }
user.password_confirmation { "password" }
end
Second, to use the same user in both factories, you'd want:
user = Factory(:user)
Factory(:app, :user => user)
Factory(:location, :user => user)
> --
> Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> You received this message because you are subscribed to the "factory_girl" mailing list.
> To post to this group, send email to factor...@googlegroups.com
> To unsubscribe from this group, send email to
> factory_girl...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/factory_girl?hl=en
>
--
Dan Croak
@Croaky
Please tell me if there is a better way to do this?
Example:
+=+=+=+=+=+=+
A cart is being created with two items where the products are of the
same type. The ProductType has a uniqueness validation.
This is what I have...
=======================
Factory.define :cart_with_two_add_items, :parent => :cart do |o|
o.after_build do |cart|
prod_type = Factory(:add_product_type) # Define locally here and
reuse below
cart.cart_items = [Factory(:cart_item,
:cart => cart,
:product => Factory
(:added_users_product,
:product_type =>
prod_type)),
Factory(:cart_item,
:cart => cart,
:product => Factory
(:added_profiles_product,
:product_type =>
prod_type))]
end
end
def test_cart_with_same_item_types
cart = Factory(:cart_with_two_add_items)
# ... Do asserts
end
This is what I have want...
=======================
Factory.define :cart_with_two_add_items, :parent => :cart do |o|
o.after_build do |cart|
cart.cart_items = [Factory(:cart_item_added_users, :cart => cart),
Factory(:cart_item_added_profiles, :cart =>
cart)]
end
end
If you want more details on the parts involved, I posted a question on
Stackoverflow with more information.
http://stackoverflow.com/questions/2015473
Thanks,
-Mark E.
On Dec 18 2009, 1:47 pm, Dan Croak <dcr...@thoughtbot.com> wrote:
> Hi,
>
> Two things.
>
> First, FG has sequences to help with the dup email addresses:
>
> Factory.sequence :email do |n|
> "user#...@example.com"
> end
>
> Factory.define :user do |user|
> user.email { Factory.next(:email) }
> user.password { "password" }
> user.password_confirmation { "password" }
> end
>
> Second, to use the same user in both factories, you'd want:
>
> user = Factory(:user)
> Factory(:app, :user => user)
> Factory(:location, :user => user)
>
>
>
> On Fri, Dec 18, 2009 at 2:10 PM, Andy Waite <andywa...@gmail.com> wrote:
> > Hi,
>
> > I'm new to using factory_girl and I'm having a little trouble
> > understanding how associations should work.
>
> > My factory.rb looks like this:
>
> > Factory.define :user do |u|
> > u.name 'John Smith'
> > u.administrator false
> > u.email_address 'john.sm...@example.com'
It is all detailed here...
http://stackoverflow.com/questions/2015473/using-factorygirl-in-rails-with-associations-that-have-unique-constraints-getti
-Mark E.
> Stackoverflow with more information.http://stackoverflow.com/questions/2015473