Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the rack env hash empty in Rails test environment?

In my Rails app, I'm accessing the env hash in one of my controller actions.

Something along the lines of:

def my_before_filter
  env['some.key'] = "Something or other"
end

This works great for my requirements.

If I start my Rails app in test environment, and visit an action like:

# /users in UsersController#index
def index
  puts env.inspect
end

Then the content of the env hash is output to the console as expected.

When I get this action from within an RSPec example, the output is an empty hash?

it 'should get the index action' do
  get :index
end

.....{}.... # rspec output

Why is the env hash empty?

I've built a dummy rails app to show this

like image 490
bodacious Avatar asked Jan 24 '12 16:01

bodacious


1 Answers

use

request.env

instead of just env within the controller code.

e.g/

def index
  puts request.env.inspect
end

Hope that helps?

BTW on another note: when working with your github repo you need to delete the public/index.html for your root route to work, when running the server.

like image 121
gef Avatar answered Sep 26 '22 01:09

gef