代码之家  ›  专栏  ›  技术社区  ›  rlandster

Rails集成测试中的变更请求环境变量

  •  7
  • rlandster  · 技术社区  · 16 年前

    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
    

    功能测试工作正常。

    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
    

    @request 是零。我怀疑这与session对象有关,但我不确定如何让它工作。

    2 回复  |  直到 16 年前
        1
  •  3
  •   Roger Ertesvag    15 年前

    您可以在集成测试中使用设置HTTPS

    https!
    

    并将主机名设置为:

    host! "sandbox.example.com"
    

    导轨指南中对此进行了说明 Rails guides

        2
  •  0
  •   Alexey Chernikov    12 年前

    对于你的情况,方法 测试\u创建 将:

    def test_create
      https!
    
      get "/", nil, { 'SERVER_NAME'] => "sandbox.example.com", 'REMOTE_USER'] => "joeuser" }
    
      assert_response :success
      [... more ...]
    end
    

    将post请求设置为原始数据也同样适用:

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