我需要在我的测试代码中生成随机的纬度和经度值,但是使用Random
类会产生重复的值导致测试失败,有什么方法可以在FactoryGirl
中生成随机的浮点数值吗?
编辑:相关的代码片段如下:
factory :location do
lat rand(-90.0..90.0)
lng rand(-180.0..180.0)
end
我需要在我的测试代码中生成随机的纬度和经度值,但是使用Random
类会产生重复的值导致测试失败,有什么方法可以在FactoryGirl
中生成随机的浮点数值吗?
编辑:相关的代码片段如下:
factory :location do
lat rand(-90.0..90.0)
lng rand(-180.0..180.0)
end
不确定为什么会出现重复值,但这个方法可能会起作用:
lat = rand(-90.0..90.0)
lng = rand(-180.0..180.0)
factory :location do
lat { rand(-90.0..90.0) }
lng { rand(-180.0..180.0) }
end
factory :address do
latitude { Faker::Address.latitude }
longitude { Faker::Address.longitude }
end