用geocoder gem测试

Flo*_*ahl 8 testing ruby-on-rails rails-geocoder

我正在使用地理编码器gem,但我不知道在哪个文件中我必须粘贴此代码.你能告诉我吗 ?

Som*_*ere 14

这段代码应该在你正在使用的任何测试框架的SETUP部分.

如果使用rspec,它应该在这里:

describe Something do
 before(:all) do 
  Geocoder.configure(:lookup => :test)

  Geocoder::Lookup::Test.add_stub(
  "New York, NY", [
    {
      'latitude'     => 40.7143528,
      'longitude'    => -74.0059731,
      'address'      => 'New York, NY, USA',
      'state'        => 'New York',
      'state_code'   => 'NY',
      'country'      => 'United States',
      'country_code' => 'US'
    }
   ]
  )
 end
end
Run Code Online (Sandbox Code Playgroud)


Jon*_*ern 10

由于您没有陈述您的测试框架,我将给出具体的答案.

我正在使用Cucumber和Rspec.虽然以上所有内容都来自@DevDude和@malandrina,但这里有一个更完整的提示,代码可以去哪里,以及如何添加反向地理编码条目(lat/lon - >地址):

将您的存根放在spec文件夹中.我创建了一个数组数组,以便我可以添加多个"查找"来删除:

spec/support/geocoder_stubs.rb
addresses = {
  "230 West 43rd St., New York City, NY 10036" => {
      'latitude' => 40.7573862,
      'longitude' => -73.9881256,
      'address' => '230 West 43rd St., New York City, NY 10036',
      'city' => 'New York City',
      'state' => 'New York',
      'state_code' => 'NY',
      'country' => 'United States',
      'country_code' => 'US'
  },
  [40.75747130000001, -73.9877319] => {
      'latitude' => 40.75747130000001,
      'longitude' => -73.9877319,
      'address' => '229 West 43rd St., New York City, NY 10036',
      'city' => 'New York City',
      'state' => 'New York',
      'state_code' => 'NY',
      'country' => 'United States',
      'country_code' => 'US'
  },
  "Worthington, OH" => {
    'latitude' => 40.09846115112305,
    'longitude' => -83.01747131347656,
    'address' => 'Worthington, OH',
    'city' => 'Worthington',
    'state' => 'Ohio',
    'state_code' => 'OH',
    'country' => 'United States',
    'country_code' => 'US'
  },
}

Geocoder.configure(:lookup => :test)
addresses.each { |lookup, results| Geocoder::Lookup::Test.add_stub(lookup, [results]) }
Run Code Online (Sandbox Code Playgroud)

在Cucumber支持文件夹中引用您的存根:

features/support/env.rb
require Rails.root.join("spec/support/geocoder_stubs")
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!