Geocoder gem测试ip请求

时间:2013-09-20 09:48:05

标签: ruby-on-rails development-environment rails-geocoder test-environments

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

应用/视图/产品/ index.html.erb

<%= render @products %>

应用/视图/产品/ _product.html.erb

<td><%= distance_between(@current_location, product.location) %></td>

@products@current_location在关联的控制器中定义:

def index
  @products = Product.all
end

并且:

unless @current_location
  if !current_user.nil?
    @current_location = current_user.location
  else
    @current_location = request.location
  end
end

distance_between方法在位置助手中定义:

def distance_between(here, there)
  t('location.distance.kilometre', count: here.distance_to(there, :km).round(1)) 
end

所以,我的测试

let(:current_location) { request.location }
it { should have_selector('td', text: distance_between(product.location, current_location))}

错误信息:

 Failure/Error: it { should have_selector('td', text: distance_between(product.location, current_location))}
 NoMethodError:
   undefined method `location' for nil:NilClass
 Shared Example Group: "product's informations" called from ./spec/requests/product_index_page_spec.rb:119
 # ./spec/requests/product_index_page_spec.rb:118:in `block (4 levels) in <top (required)>'
 # ./spec/support/products/index.rb:31:in `block (2 levels) in <top (required)>'

在生产中,地理编码器gem可以请求用户的ip,并使用request.location呈现他的位置,但我不知道如何在测试或开发环境中这样做。你能帮我纠正我的代码吗?

0 个答案:

没有答案