Skip to content
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

Allow usage of relations in dependant attributes #48

Open
alexandru-calinoiu opened this issue Oct 24, 2019 · 4 comments
Open

Allow usage of relations in dependant attributes #48

alexandru-calinoiu opened this issue Oct 24, 2019 · 4 comments

Comments

@alexandru-calinoiu
Copy link
Contributor

Noticed that when using a relation as dependency in creating an attribute the relation always comes in nil, any way I can achieve this?

I have tables that have some of the data denormalized and some attributes need to have the same value on the entity and on the relation.

@solnic
Copy link
Member

solnic commented Oct 24, 2019

Can you show an example? I'm not sure I understand.

@alexandru-calinoiu
Copy link
Contributor Author

Factory.define :scheduled_position do |f|
  # TODO: figure out a way to set position_id to shift_position.position_id
  f.association(:position)
end

In the above code, I expected scheduled_position#position_id to be set the position#id

Does this make it any clearer?

@solnic
Copy link
Member

solnic commented Dec 10, 2019

@alexandru-calinoiu kind of yes, a sample setup that shows the problem would be more helpful though. Are you saying that in this case factory[:scheduled_position].position_id returns nil?

@masterT
Copy link

masterT commented Oct 31, 2022

I think the same problem, I want to create consistent entities for my tests.

Example:

  • Entity web_resource has the attribute uri
  • Entity web_resource_content has the attribute uri and the association to web_resource

In my tests, I would like to be able to create valid factories by default (without passing attributes, and entities) to the factory call. I would like to be able to create a web_resource_content and web_resource using the same uri like this.

web_resource_content = Factory[:web_resource_content]
web_resource_content.uri == web_resource_content.resource_content.uri
# => true

This is possible with FactoryBot like this:

factory :web_resource_content do
  uri { Faker::Internet.url }
  web_resource { association :web_resource, uri: uri }
end

I don't know if this is something that can be interested to have in this library. It could be using this syntax:

Factory.define(:web_resource_content) do |f|
  f.association(:web_resource) { |uri| { uri: uri } }
  f.uri { fake(:internet, :url) }
end

or

Factory.define(:web_resource_content) do |f|
  f.web_resource { |uri| association(:web_resource, attributes: { uri: uri }) }
  f.uri { fake(:internet, :url) }
end

I don't know the implementation details of the library, this is just an example from a consumer perspective. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants