rom-factory
rom-factory copied to clipboard
Allow usage of relations in dependant attributes
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.
Can you show an example? I'm not sure I understand.
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?
@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
?
I think the same problem, I want to create consistent entities for my tests.
Example:
- Entity
web_resource
has the attributeuri
- Entity
web_resource_content
has the attributeuri
and the association toweb_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. 🙂