New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
attributes_for(:factory, some_association: foo) returns nil for :some_association #1292
Comments
That is the expected behavior for |
Hi @composerinteralia , thanks for your quick response. I think this issue is different from #687 , #450, #408, #359 or other related issues. Those issues are about The problem here is that # Assume foo and bar are *not* attributes of Job
attributes_for(:job, foo: :bar, bar: :foobar) # returns { foo: :bar, bar: :foobar }
attributes_for(:job, foo: :bar, bar: :foobar, employee: some_employee) # returns { foo: :bar, bar: :foobar } In other words, I don't want |
It discards it because it is an association. The fact that the value is passed in as an override doesn't change how factory_bot/lib/factory_bot/strategy/attributes_for.rb Lines 4 to 6 in 99ac024
Could you share your use case? Perhaps we can find a workaround. We can also discuss the current behavior more, but changing it would be non-trivial and I would want to make sure there was a common use case before exploring that. |
My use case is that I have some methods that create the models, and I'd like to test those:
Now, I'd like to test this
The annoying part is that, as a user, it is not obvious that by declaring an association you render the attribute unsettable in the |
I can understand that, and maybe it is not worth fixing. However, I think the current behavior is not intuitive. Perhaps |
Yeah, I see your point. Thanks for that example. |
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:
attributes_for
ignores any explicitemployee
assignments:build
andcreate
do not suffer from this problem. Removing theassociation
annotation from the second factory solves the problem, but then I can't have a default association forcreate
.The following spec fails on current master:
Note that the following does work:
The text was updated successfully, but these errors were encountered: