thoughtbot / thoughtbot/factory_bot
attributes_for(:factory, some_association: foo) returns nil for :some_association
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
I have searched the issue list for something similar, but I have not found the exact same issue.
The problem is that, given these two models:
factory :employee do
# various attributes
end
factory :job do
association :employee, strategy: :build
end
attributes_for ignores any explicit employee assignments:
attributes_for(:job, employee: some_other_employee)[:employee] # this is nil!
build and create do not suffer from this problem. Removing the association annotation from the second factory solves the problem, but then I can't have a default association for create.
The following spec fails on current master:
it "allows specifying association values" do
sub = attributes_for(:post, user: User.new)
expect(sub[:user]).not_to eq nil
end
Note that the following does work:
it "allows overriding association keys" do
sub = attributes_for(:post, user_id: 5)
expect(sub[:user_id]).to eq 5
end
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the attributes_for entry point and reproduce the failing spec described in the issue with an explicit association value such as User.new. Trace how the association declaration affects the returned attributes, then verify that explicit association values are preserved while the existing association default still works for build or create.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100