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
If you have any data that you don't want to be changed, the simplest approach is to basically treat it as fixtures. You can either use Rails' own fixtures mechanism (see documentation in the official Rails Guides: https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures) or build your own simple fixture importer: