【问题标题】:Rails Geocoder Testing with rspec使用 rspec 进行 Rails 地理编码器测试
【发布时间】:2015-09-09 21:51:16
【问题描述】:

我正在尝试将我的 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

纬度/经度已正确进行地理编码并存储在纬度/经度中。但是,当我尝试时:

Market.near(params[:search])

它返回 nil.. 但是,如果我只使用查找 => :google 它就像我想要的那样工作。有没有人以前做过这个工作,特别是地理编码器的近方法?

【问题讨论】:

  • 你有想过这个吗?我现在遇到了完全相同的问题
  • @AlexNeigher 请参阅下面的答案。

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


【解决方案1】:

我最终在一个新项目上回到了这个问题上并想通了。

地理编码器上的文档实际上声明哈希必须具有字符串键而不是符号。 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

而不是我在原始帖子中的做法:

: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 构建的。

【讨论】:

  • :heart: :heart: :heart:
  • 最好的解决方案是使用vcr来存根所有http请求
【解决方案2】:

我发现了一种更简单的方法,即默认情况下使用相同的值对所有内容进行存根:

# 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

来源:https://github.com/alexreisner/geocoder#testing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2012-01-18
    • 2021-11-15
    相关资源
    最近更新 更多