在rspec中使用address_components存根地理编码器

时间:2017-07-21 07:05:51

标签: ruby-on-rails rails-geocoder

我尝试编写rspec来测试一些使用gem Geocoder的代码。 locationGeocoder::Result::Google对象,我的代码如下:

address = location.address_components_of_type(:sublocality_level_1).first&.dig('long_name')

我实现了一个点(参考here): ```红宝石

Geocoder.configure(lookup: :test)
Geocoder::Lookup::Test.set_default_stub(
  [
    {
      'latitude'     => 35.6691084,
      'longitude'    => 139.6009546,
      'address'      => 'Address',
      'state'        => 'State',
      'city'         => 'City',
      'types'        => ['political'],
      'address_components' => [
        {
          'types' => ['sublocality_level_1'],
          'long_name' => 'level_1',
        }
      ]
    },
  ]
)

我试着在运行rspec时检查并检查,我得到:

(byebug) location.class
Geocoder::Result::Test
(byebug) location.address_components
[{"types"=>["sublocality_level_1"], "long_name"=>"level_1"}]
(byebug) location.respond_to?(:address_components_of_type)
false

location.respond_to?(:address_components_of_type)返回false,我无法按照我的预期调用location.address_components_of_type(:sublocality_level_1).first&.dig('long_name')

location运行时,rspec是Geocoder::Result::Test对象

所以,你能帮我解决这个问题吗?非常感谢

Geocoder版本:1.4.3 Rails版本:5.0.2

0 个答案:

没有答案