我正在使用geokit-rails gem根据通过#geocode_ip_address (https://github.com/geokit/geokit-rails#ip-geocoding-helper)登录的用户的IP地址执行地理定位。
但是,我很难找到一种通过Rails 5 IntegrationTest来测试这个问题的方法。在模拟登录时,我需要一种提供多个远程IP地址的方法,但在以下问题上我陷入了困境:
我最初的方法是跳过它,将:geo_location信息放在session哈希中,但是这在Rails 5中似乎已经消失了。
有人有过类似的设计经验吗?
发布于 2019-08-02 08:38:33
根据geokit-rails lookup.rb#L44中ip查找的实现,您可以简单地将remote_ip方法存根到request对象上。
geokit-rails在基于IP的基础上找到了一个geo_location,它将它作为geo_location对象存储在session中。您可能不希望这种“缓存”行为出现在开发/测试中,所以我在欺骗IP之前删除对象。
这里有一个实例实现:
class SomeController < ApplicationController
prepend_before_action :spoof_remote_ip if: -> { %w(development test).include? Rails.env }
geocode_ip_address
def index
@current_location = session[:geo_location]
end
def spoof_remote_ip
session.delete(:geo_location) if session[:geo_location]
request.env["action_dispatch.remote_ip"] = ENV['SPOOFED_REMOTE_ADDR']
end
end# .env
SPOOFED_REMOTE_ADDR = "xx.xxx.xx.xxx"https://stackoverflow.com/questions/42127235
复制相似问题