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

…衆ロ難τιáo~ 提交于 2019-12-09 16:43:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8990419/why-is-the-rack-env-hash-empty-in-rails-test-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!