Ruby on rails 3.2 远程地址=方法不';我不能在集成测试中工作。这种方法需要什么?

Ruby on rails 3.2 远程地址=方法不';我不能在集成测试中工作。这种方法需要什么?,ruby-on-rails-3.2,integration-testing,Ruby On Rails 3.2,Integration Testing,我正在Rails中进行集成测试。我想模拟来自任意ip地址的请求。到目前为止,我了解到有两种方法可以做到这一点: remote_addr=(remote_ip) ## the first get("/", nil, {"REMOTE_ADDR" => "999.99.99"}) ## the second 但是第一个在集成测试中不起作用。它不设置远程地址: test("some integration test") do r_a = "999.99.99" puts remo

我正在Rails中进行集成测试。我想模拟来自任意ip地址的请求。到目前为止,我了解到有两种方法可以做到这一点:

remote_addr=(remote_ip) ## the first
get("/", nil, {"REMOTE_ADDR" => "999.99.99"}) ## the second
但是第一个在集成测试中不起作用。它不设置
远程地址

test("some integration test") do
   r_a = "999.99.99"

   puts remote_addr
     remote_addr=(r_a)  ## neither before get()...
   puts remote_addr

   get("/")  ## get request

     remote_addr=(r_a)  ## nor after get() it doesn't set remote_addr

   assert_equal(r_a, assigns(:r_a))  ## assertion test 
end
现在我运行测试。这是一个结果。请注意,值在以下时间前后发生变化:

# Running tests:

127.0.0.1
999.99.99
F

Finished tests in 1.109375s, 0.9014 tests/s, 5.4085 assertions/s.

  1) Failure:
test_some_integration_test(UserFlowsTest) [test/integration/user_flows_test.rb:45]:
<"999.99.99"> expected but was
<"127.0.0.1">.
结果呢。请注意,该值在
remote\u addr()
中未更改,但在
assigns(:r\u a)
中更改:

的源代码如下:

它必须改变,但事实并非如此

  • 为什么
    remote\u addr=
    没有在集成测试中设置远程ip

  • 那么为什么需要这种方法呢

  • 是否仍然可以在集成测试中使用它,或者第二种方法是唯一的选择


  • 因为我在这个问题上写了很多东西,所以我在这里发布了一些更新

    如果您的集成测试具有操作重定向(例如,从
    创建
    操作
    重新检测到(“显示”)
    操作),并且您模拟了来自任意IP的请求(
    env['REMOTE\u ADDR']
    ),请不要在集成测试中使用)。因此,您希望在两个操作中保持相同的
    remote\u addr

    follow\u重定向!()
    不会持久化
    env['REMOTE\u ADDR']
    设置<代码>环境['REMOTE\u ADDR']
    在重定向到操作中重置

    而是使用方法。这样,例如:

    # your redirection assertion...
    assert_response(:redirect)
    
    # is followed by this
    request_via_redirect("GET", 
       url_for(:action => "show", :id => assigns(:model).id), 
       nil,
       {"REMOTE_ADDR" => "999.99.99"})
    
    使用此方法,您可以将
    env['REMOTE\u ADDR']
    设置转发到下一个操作

    # Running tests:
    
    127.0.0.1
    127.0.0.1
    assigns(:r_a) = 999.99.99
    .
    
    Finished tests in 1.296875s, 0.7711 tests/s, 7.7108 assertions/s.
    
    # File actionpack/lib/action_dispatch/testing/test_request.rb, line 55
    def remote_addr=(addr)
      @env['REMOTE_ADDR'] = addr
    end
    
    # your redirection assertion...
    assert_response(:redirect)
    
    # is followed by this
    request_via_redirect("GET", 
       url_for(:action => "show", :id => assigns(:model).id), 
       nil,
       {"REMOTE_ADDR" => "999.99.99"})