luckyframework / luckyframework/avram

Add ability to pass block to factory attributes

Open
#808 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

The recommended way to override attributes of a factory is something like this

```crystal
CommentFactory.create &.post_id(new_post.id).text(sequence("blah"))
```

Now, if `CommentFactory` sets this `post_id` in it's initialize with a call to a `PostFactory` that will end up with two posts made which is not intended and can add up to a lot of random records being created in large test suites.

I believe this is why libraries like `FactoryBot` switched to requiring all attributes to be passed values through a block. If we switched to something similar, the factory would look like

```crystal
class CommentFactory < Avram::Factory
def initialize
text { "not blah" }
post_id { PostFactory.create.id }
end
end
```

And when the usage I added at the very beginning is used, we could have it set up to where the block passed to `post_id` is not called resulting in no extra records made.

Thoughts?

Contributor guide

Open the contributing guide

Research direction

Start by reviewing Avram::Factory and the CommentFactory/PostFactory examples described here, then trace how attribute overrides are currently evaluated. Clarify the intended block semantics with maintainers, including whether skipped defaults prevent extra records, and add coverage showing an overridden post_id does not create an additional Post.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.