I would like to know how to use the same array in multiple factories using rspec in Rail, does any body know a good solution?
Currently, I have the array bellow that I have to repeat in every factory that needs to enter the lat/log.
coordinates = [
{latitude: 50.0, longitude: -100.0},
{latitude: 50.1, longitude: -100.1},
{latitude: 50.2, longitude: -100.2},
{latitude: 50.3, longitude: -100.3}
]
Then, to attribute a value I do a .sample and insert into the field:
coordinate = coordinates.sample
latitude { coordinate[:latitude] }
longitude { coordinate[:longitude] }
I would like to keep these coordinates "padronized" so I can be sure where it is going to be. That's because later down the road I check if they fall inside a polygon or not.
I am currently using: rails 5.2.3 rspec-rails 3.8.0 factory_bot_rails 5.0.1
FactoryBot is about avoiding having fixed testing data. Instead, write a factory which will generate valid coordinates.
Rather than hard coding your coordinates so they fit inside a hard coded polygon, generate a polygon which contains the coordinates as you need. Do this with a trait. Traits let different tests use the same factory in different ways without any risk of interfering with each other.
Then ask for a polygon which contains a coordinate when you need it.
This can also work the other way around, add a trait to the
coordinatesfactory which is guaranteed to be inside a polygon.