thoughtbot / thoughtbot/factory_bot
has_many relations
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
I'd like to be able to clearly, safely, and idiomatically create an object and its many relations. For example:
new_author = create(:author)
distinguished_author = create(:author, with_article_count: 3)
assert_equal new_author.articles.count, 0
assert_equal distinguished_author.articles.count, 3
The above can currently be done using a transient property and some hooks, but it's kinda kludgy and hard to get right. Let's make this somewhat common case easier and less error prone.
It could look like this:
FactoryBot.define do
factory :author do
association(:article, relation: :has_many)
end
end
Or it could allow overrides:
FactoryBot.define do
factory :author do
association(:unique_article, relation: :has_many) do |user, offset|
title { "How to win, part #{offset + 1}" } # sets the title for the article
end
end
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 with the linked has_many associations cookbook page and review the transient-property and hook approach described there. Compare the proposed association APIs against the shown author/article assertions; done means creating an author with a requested number of articles is clear, safe, and less error prone.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100