2010-06-08 2 views

답변

7

:

When /^the request ip address is "([^\"]*)"$/ do |ip_address| 
    ENV['RAILS_TEST_IP_ADDRESS'] = ip_address 
    end 

application_controller.rb :

before_filter :mock_ip_address 

    def mock_ip_address 
    if Rails.env == 'cucumber' || Rails.env == 'test' 
     test_ip = ENV['RAILS_TEST_IP_ADDRESS'] 
     unless test_ip.nil? or test_ip.empty? 
     request.instance_eval <<-EOS 
      def remote_ip 
      "#{test_ip}" 
      end 
     EOS 
     end 
    end 
    end 
+1

나는 레일 3.1 그리고 난 IP로 remote_ip 변경했다. 이것은 rubygeocoder.com gem과 잘 작동합니다. 게시 해 주셔서 감사합니다! – jspooner

+3

이것은 시원하고 유용했습니다. 그러나 다른 독자들에게주는 메모는'spec/support/remote_ip_monkey_patch.rb'와 같은 파일로 ApplicationController를 열어 원숭이에게 패치하는 것이 가장 좋습니다. 더 깨끗하고 멋지게 보입니다. –

5

Leventix의 라몬의 솔루션을 내 혼합 :

사양/지원/remote_ip_monkey_patch.rb

module ActionDispatch 
    class Request 

    def remote_ip_with_mocking 
     test_ip = ENV['RAILS_TEST_IP_ADDRESS'] 

     unless test_ip.nil? or test_ip.empty? 
     return test_ip 
     else 
     return remote_ip_without_mocking 
     end 
    end 

    alias_method_chain :remote_ip, :mocking 

    end 
end 
+0

'ENV [ 'RAILS_TEST_IP_ADDRESS']'객체가 어떤 종류인지 알려주시겠습니까? 그냥 문자열인가요? 나는''123.123.123.123 '또는 무엇인가 시도 할 것이다. –

관련 문제