Rails 使用 rspec 进行地理编码器测试
Rails Geocoder Testing with rspec
我正在尝试设置我的 RSpec 测试以使用存根而不是使用网络来进行地理编码。
我添加了这个:
before(:each) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"Los Angeles, CA", [{
:latitude => 34.052363,
:longitude => -118.256551,
:address => 'Los Angeles, CA, USA',
:state => 'California',
:state_code => 'CA',
:country => 'United States',
:country_code => 'US'
}],
)
结束
我正在使用 FactoryGirl 创建测试数据,如下所示:
FactoryGirl.define do
factory :market do
city 'Los Angeles'
state 'CA'
radius 20.0
end
end
latitude/longitude 已正确地理编码并存储在 latitude/longitude 中。但是,当我尝试时:
Market.near(params[:search])
它 returns 零.. 但是,如果我只使用查找 => :google 它会按照我的意图工作。有没有人以前做过这个工作,特别是地理编码器的 near 方法?
我最终在一个新项目上回到这个问题上并弄明白了。
geocoder 上的文档实际上指出哈希必须具有字符串键而不是符号。 geocoder docs - see notes
即
before(:each) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"Los Angeles, CA", [{
"latitude" => 34.052363,
"longitude" => -118.256551,
"address" => 'Los Angeles, CA, USA',
"state" => 'California',
"state_code" => 'CA',
"country" => 'United States',
"country_code" => 'US'
}],
)
end
而不是我原来的做法 post:
即:latitude => 34.052363
我最终做了一些更有活力的事情:
# frozen_string_literal: true
module GeocoderStub
def self.stub_with(facility)
Geocoder.configure(lookup: :test)
results = [
{
'latitude' => Faker::Address.latitude.first(9),
'longitude' => Faker::Address.longitude.first(9)
}
]
queries = [facility.full_address, facility.zip]
queries.each { |q| Geocoder::Lookup::Test.add_stub(q, results) }
end
end
在我的工厂:
require './spec/support/geocoder_stub'
FactoryGirl.define do
factory :facility do
name { Faker::Company.name }
rating { rand(1..5) }
street { Faker::Address.street_address }
city { Faker::Address.city }
state { Faker::Address.state }
zip { Faker::Address.zip_code }
after(:build) { |facility| GeocoderStub.stub_with(facility) }
end
end
这为每个为完整地址(在设施中定义的方法)和 zip 构建的设施工厂添加了一个地理编码器存根。
我发现了一种更简单的方法,即默认情况下使用相同的值对所有内容进行存根:
# test/test_helper.rb
Geocoder.configure(lookup: :test)
Geocoder::Lookup::Test.set_default_stub([{ coordinates: [40.7143528, -74.0059731] }])
此外,为了避免不必要的调用,限制回调也是一个好主意:
class Account < ActiveRecord
after_validation :geocode, if: ->(obj) { obj.address.present? and obj.address_changed? }
end
我正在尝试设置我的 RSpec 测试以使用存根而不是使用网络来进行地理编码。
我添加了这个:
before(:each) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"Los Angeles, CA", [{
:latitude => 34.052363,
:longitude => -118.256551,
:address => 'Los Angeles, CA, USA',
:state => 'California',
:state_code => 'CA',
:country => 'United States',
:country_code => 'US'
}],
)
结束
我正在使用 FactoryGirl 创建测试数据,如下所示:
FactoryGirl.define do
factory :market do
city 'Los Angeles'
state 'CA'
radius 20.0
end
end
latitude/longitude 已正确地理编码并存储在 latitude/longitude 中。但是,当我尝试时:
Market.near(params[:search])
它 returns 零.. 但是,如果我只使用查找 => :google 它会按照我的意图工作。有没有人以前做过这个工作,特别是地理编码器的 near 方法?
我最终在一个新项目上回到这个问题上并弄明白了。
geocoder 上的文档实际上指出哈希必须具有字符串键而不是符号。 geocoder docs - see notes
即
before(:each) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"Los Angeles, CA", [{
"latitude" => 34.052363,
"longitude" => -118.256551,
"address" => 'Los Angeles, CA, USA',
"state" => 'California',
"state_code" => 'CA',
"country" => 'United States',
"country_code" => 'US'
}],
)
end
而不是我原来的做法 post:
即:latitude => 34.052363
我最终做了一些更有活力的事情:
# frozen_string_literal: true
module GeocoderStub
def self.stub_with(facility)
Geocoder.configure(lookup: :test)
results = [
{
'latitude' => Faker::Address.latitude.first(9),
'longitude' => Faker::Address.longitude.first(9)
}
]
queries = [facility.full_address, facility.zip]
queries.each { |q| Geocoder::Lookup::Test.add_stub(q, results) }
end
end
在我的工厂:
require './spec/support/geocoder_stub'
FactoryGirl.define do
factory :facility do
name { Faker::Company.name }
rating { rand(1..5) }
street { Faker::Address.street_address }
city { Faker::Address.city }
state { Faker::Address.state }
zip { Faker::Address.zip_code }
after(:build) { |facility| GeocoderStub.stub_with(facility) }
end
end
这为每个为完整地址(在设施中定义的方法)和 zip 构建的设施工厂添加了一个地理编码器存根。
我发现了一种更简单的方法,即默认情况下使用相同的值对所有内容进行存根:
# test/test_helper.rb
Geocoder.configure(lookup: :test)
Geocoder::Lookup::Test.set_default_stub([{ coordinates: [40.7143528, -74.0059731] }])
此外,为了避免不必要的调用,限制回调也是一个好主意:
class Account < ActiveRecord
after_validation :geocode, if: ->(obj) { obj.address.present? and obj.address_changed? }
end