Ruby on rails Rails集成测试中的更改请求环境变量

Ruby on rails Rails集成测试中的更改请求环境变量,ruby-on-rails,environment-variables,integration-testing,Ruby On Rails,Environment Variables,Integration Testing,我编写了一个功能测试,它更改了请求对象的一些环境变量,以模拟用户已登录 require 'test_helper' class BeesControllerTest < ActionController::TestCase # See that the index page gets called correctly. def test_get_index @request.env['HTTPS'] = "on" @request.env['SERVER_NAM

我编写了一个功能测试,它更改了请求对象的一些环境变量,以模拟用户已登录

require 'test_helper'
class BeesControllerTest < ActionController::TestCase

  # See that the index page gets called correctly.
  def test_get_index

    @request.env['HTTPS'] = "on"
    @request.env['SERVER_NAME'] = "sandbox.example.com"
    @request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER

    get :index
    assert_response :success
    assert_not_nil(assigns(:bees))
    assert_select "title", "Bees and Honey"
  end
end
需要“测试助手”
类BeesControllerTest
功能测试工作正常

现在我想做一些类似的事情,作为集成测试的一部分。以下是我尝试过的:

require 'test_helper'
class CreateBeeTest < ActionController::IntegrationTest
  fixtures :bees

  def test_create
    @request.env['HTTPS'] = "on"
    @request.env['SERVER_NAME'] = "sandbox.example.com"
    @request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER

    https?

    get "/"
    assert_response :success
    [... more ...]
  end
end
需要“测试助手”
类CreateBeeTest

我收到一个错误,抱怨
@request
为零。我怀疑这与会话对象有关,但我不确定如何使其工作。

您可以在与的集成测试中设置HTTPS

https!
并将主机名设置为:

host! "sandbox.example.com"
这可能相当于你想做什么


这在Rails指南中进行了描述。您可以通过parameters to post方法更改请求变量

对于您的情况,方法测试将是:

将post请求设置为原始数据也适用于此:

post root_path, nil, { 'RAW_POST_DATA' => 'some string' }

上的行
@request.env['HTTPS']=”抱怨您不能将
env
方法应用于nil值。下面是一个三年前提交给Rails的补丁,它解决了这个问题:但是如何设置例如
request.remote\u ip
?对于
ActionDispatch::Integration::Session
post root_path, nil, { 'RAW_POST_DATA' => 'some string' }