Accessing associations created by factories via capture_fields

3 views
Skip to first unread message

Gabe

unread,
Nov 12, 2009, 1:42:30 PM11/12/09
to pickle
It seems that any associated models created by a factory need to be
queried and cached via find_model() to be accessible later by model()

Given the following Scenario:

Scenario: Parent returning for a new school year
Given I am logged in as an existing parent
And a student exists with year: year "last", account: the account
And I inspect the parent
And I inspect the student

And the two custom step definitions:
Given /^I am logged in as an existing #{capture_model}(?: with #
{capture_fields})?$/ do |name, fields|
create_model(name, fields)
u = model(name)

When "I go to the login page"
When "I fill in \"email\" with \"#{u.email}\""
When "I fill in \"password\" with \"password\""
When "I press \"Log In\""

# This caches the association created by the factory so calls to
model("account: the account") work
# Then "an account should exist"
end

Given /^I inspect #{capture_model}$/ do |name|
puts model(name).inspect
end

Then the student's account returns as nil:
#<User id: 121, account_id: 128, ...>
#<Student id: 32, account_id: nil, ...>

However, if I uncomment the "an account should exist" line in my step
(thus causing a "find" operation to query and refresh Pickle's cache):
#<User id: 124, account_id: 131, ...>
#<Student id: 34, account_id: 131, ...>

Solutions?
1) Documentation update that says pickle will not know about
associated objects not being available via model() until a find_model
() is run
2) Constantly query all records after a create() operation (the "joke"
option, unless you have an idea on how to make this work
pragmatically)


Thanks for all the awesome library :)

Gabe

unread,
Nov 12, 2009, 1:44:44 PM11/12/09
to pickle
I foolishly left out my factory girl definitions, which might make
this make sense. In this case, "parent" is a type of User, and many
Users belong to one Account. In the above login step, it creates a
parent (User), and the factory is setup to automatically create an
Account for that user

Factory.define :user do |u|
u.association :account
u.first_name 'John'
u.last_name 'Doe'
u.sequence(:email) { |n| "person#{n}@example.com" }
u.password 'password'
u.password_confirmation { |u| u.password }
end

Factory.define :parent, :parent => :user do |u|
u.association :account
end

Factory.define :account do |a|
end
Reply all
Reply to author
Forward
0 new messages