Factory_bot: Dependent Associations

Created on 15 Aug 2012  Â·  11Comments  Â·  Source: thoughtbot/factory_bot

FactoryGirl's support for dependent attributes needs to be extended to work with associations. Currently, you have to use ugly workarounds like this: Stack Overflow: Get two associations within a Factory to share another association

Most helpful comment

I'm curious if there's any intent to work on this (I know it's pretty old) - I've got some slow tests that would be sped up drastically by being able to defers to whatever the main factory's strategy instead of use create every time because some tests will require the full create.

All 11 comments

+1

What were you thinking for a syntax?

On Aug 18, 2012, at 2:22, mikelewis [email protected] wrote:

+1

—
Reply to this email directly or view it on GitHub.

Is there any reason it can't be exactly the same as the syntax for regular dependent attributes?

factory :foo do
  bar_relationship
  dependent_relationship { FactoryGirl.build(:dependent_relationship, bar_relationship: bar_relationship) }
end

Although, perhaps a cleaner way would be to use the syntax suggested in the StackOverflow post I referenced:

FactoryGirl.define do

  factory :relationship do
    association :guardian
    association :student, :school => self.guardian.school
    relationship_type RelationshipType.first
  end

end

The first syntax will work perfectly fine; in fact, if you're using FactoryGirl 3.2.0+, you can just do:

factory :foo do
  bar_relationship
  dependent_relationship { build(:dependent_relationship, bar_relationship: bar_relationship) }
end

The latter syntax would be very difficult to do due to the dynamic nature of calculating values from the association, but the former works fine.

Good luck!

Thanks Josh, that works well for me. One question: this specifies the "build" strategy for the dependent relationship - is there a way to do this in a way that defers to whatever the main factory's strategy is?

Thanks!

@hmayer-rp there's not a good way to do that sadly. It's one of the last big problems that I've had trouble with figuring out how to do (not so much from a code standpoint but from what makes sense). The discussion was started here https://github.com/thoughtbot/factory_girl/issues/458 with a link to a branch I started a while ago (https://github.com/thoughtbot/factory_girl/tree/produce-association) that has some ideas. I haven't rebased against master but if you're interested in trying this out locally, I could get that branch up-to-date and you could try it locally to see how it feels and if you run into any bugs...

Let me know and I should be able to tackle this on Friday!

Wow, thanks for the super fast response!

I'd love to try this out. We actually are pretty terrible about our factory usage in some ways - pretty much everything is a create, which as you might expect makes our tests a lot slower (though also easier to write - have definitely had the occasional gotcha related to not doing the full create). So being able to make dependent relationships strategy-agnostic would help us move in that direction.

However i have to admit that i'm not likely to be any use to you for the next couple of weeks (deadlines, etc.) So I wouldn't want you to rush to get that branch up to date only to have it languish without any use. We're planning to take on some technical debt in the next month or so, and i'd like to spend a bit of that time auditing and tweaking our factory usage - but i probably won't really get to this until then.

Can I ping you when we're moving that direction and see if you still have time?

And thanks again for the excellent gem and the quick turnaround.

@hmayer-rp that sounds perfect; I've been putting it off (as you can see from the fact that I haven't touched it in five months or so) but definitely ping me on this thread when you're ready. I'll refactor a bit and get it up-to-date against master before that point regardless and try it out on some of my own stuff too.

Any news on this feature ? It would really be awesome to be able to declare associations with dependent attributes and respect initial strategy

@Systho nothing of note yet, but I'll be sure to tie the commit(s) to the appropriate PRs and issues once there's progress made!

I'm curious if there's any intent to work on this (I know it's pretty old) - I've got some slow tests that would be sped up drastically by being able to defers to whatever the main factory's strategy instead of use create every time because some tests will require the full create.

Was this page helpful?
0 / 5 - 0 ratings