How do I make fun of the IP address in cucumbers / capybara?

I use Cucumber and Capybara, and I need a way to simulate the request IP address, for example:

Given the request ip address is "10.1.2.3" 
+4
source share
2 answers

I solved this by passing the IP address in the environment variable:

  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 
+7
source

My combination of Leventix and Ramon solutions:

specifications / support / 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 
+5
source

Source: https://habr.com/ru/post/1312182/


All Articles