Changing query environment variables when testing Rails integration

I wrote a functional test that changes some environment variables of the request object to simulate the user logged in.

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 

Functional test is working fine.

Now I want to do something similar, as part of integration testing. Here is what I tried:

 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 

I get a message that @request is zero. I suspect this has something to do with the session object, but I'm not sure how to make it work.

+7
ruby-on-rails environment-variables integration-testing
source share
2 answers

You can set HTTPS in integration tests with

 https! 

And set the host name with:

 host! "sandbox.example.com" 

What could be equivalent to what you want to do?

This is described in the Rails Rails guide.

+2
source share

You can change the request variables using the parameters to submit the method.

In your case, the test_create method will look like this:

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

The same thing works for setting up a mail request on raw data:

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

All Articles