Ruby on rails Geocoder gem测试ip请求

Ruby on rails Geocoder gem测试ip请求,ruby-on-rails,development-environment,rails-geocoder,test-environments,Ruby On Rails,Development Environment,Rails Geocoder,Test Environments,我想在我的视图中测试以下行: app/views/products/index.html.erb app/views/products/_product.html.erb @产品和@当前位置在相关的控制器中定义: def索引 @products=Product.all 结束 以及: 除非@current\u位置 如果!当前_user.nil? @当前位置=当前用户位置 其他的 @当前位置=request.location 结束 结束 distance\u-between方法在位置帮

我想在我的视图中测试以下行:

app/views/products/index.html.erb


app/views/products/_product.html.erb


@产品
@当前位置
在相关的控制器中定义:

def索引
@products=Product.all
结束
以及:

除非@current\u位置
如果!当前_user.nil?
@当前位置=当前用户位置
其他的
@当前位置=request.location
结束
结束
distance\u-between
方法在位置帮助器中定义:

def之间的距离(这里,那里)
t('location.distance.km',计数:这里。距离(那里:km)。圆形(1))
结束
因此,这里有我的测试:

let(:当前位置){request.location}
它{应该有_选择器('td',text:distance_-between(product.location,current_-location))}
以及错误消息:

失败/错误:它{应该有_选择器('td',text:distance_-between(product.location,current_-location))}
命名错误:
nil:NilClass的未定义方法“location”
共享示例组:从./spec/requests/product\u index\u page\u spec.rb:119调用“产品信息”
#./spec/requests/product\u index\u page\u spec.rb:118:in'block(4级)in'
#./spec/support/products/index.rb:31:in'block(2层)in'

在生产中,geocoder gem可以通过
request.location
请求用户的ip,并显示其位置,但我不知道在测试或开发环境中如何做到这一点。你能帮我更正代码吗?

嗨,弗洛,你确定你给我们展示了正确的测试吗?在您的测试中,它是request.location,在您的错误中,它是当前位置。根据显示的错误,在您的测试中,它看起来好像产品为零。好的,谢谢,我更正了它(我想简化示例,但我忘了相应地更正错误…)你在哪里定义你在距离之间使用的产品实例(product.location?嗨,Nico,我更新了我的问题。嗨,你仍然被这个错误困扰着吗?在这种情况下,你能不能显示包含以下行的整个文件:let(:current_location){request.location}it{should have_selector('td',text:distance_-between(product.location,current_-location))}