【问题标题】:Rails Rspec and Geocoder IP lookupRails Rspec 和 Geocoder IP 查找
【发布时间】:2017-08-11 11:54:02
【问题描述】:

Geocoder DOCS 解释如何使用街道地址伪造 Geocoder 查找,但它似乎不适用于 IP 地址。

这是我的规范/支持/geocoder.rb

Geocoder.configure(:lookup => :test)

Geocoder::Lookup::Test.add_stub(
  "1.1.1.1", [
  {
    'latitude'     => 10,
    'longitude'    => 10,
    'address'      => 'Test Address',
    'state'        => 'Test State',
    'state_code'   => 'TS',
    'country'      => 'Test Country',
    'country_code' => 'TC'
  }
]
)

Geocoder::Lookup::Test.set_default_stub(
  [
    {
      'latitude'     => 10,
      'longitude'    => 10,
      'address'      => 'Test Address',
      'state'        => 'Test State',
      'state_code'   => 'TS',
      'country'      => 'Test Country',
      'country_code' => 'TC'
    }
  ]
)

这是我的服务:

if params[:ip]
  lat = Geocoder.search(params[:ip])[0].latitude
  lng = Geocoder.search(params[:ip])[0].longitude
  venues = venues.near([lat, lng], APP_CONFIG['geo_location_radius'], order: 'average_rating DESC')
end

这是我的服务规范:

context "find featured venues by ip" do
  let(:params) do
    {
      date:     Date.today,
      featured: true,
      ip:       '1.1.1.1',
    }
  end

  it do
    aggregate_failures do
      expect(raw_venues.map { |v| v.id }.sort).to eq [venue1.id]
    end
  end

end

如果我在 it 块中使用 pry 输入并尝试 Geocoder.search('1.1.1.1'),我会得到一个“真实”对象响应,其中包含澳大利亚的位置而不是我的存根,而如果我输入类似 @ 的内容,我会正确获得我的存根987654327@..

如何设置存根响应以使用 IP 查找?

【问题讨论】:

    标签: ruby-on-rails rspec rails-geocoder


    【解决方案1】:

    解决方法很简单:

    Geocoder.configure(ip_lookup: :test)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      相关资源
      最近更新 更多