3

I have a Rails app (3.2.12) that I wanted to add locale switching via HTTP Accept-Language header.

What I did to achieve that:

I added rack-contrib to my Gemfile:

gem 'rack-contrib', require: 'rack/contrib'

ran bundle install, added the middleware to my config/application.rb:

config.middleware.use Rack::Locale

and inspect the request env my controller:

puts request.env.keys.select{|v| v=~/rack/ }

The spec I run is a controller spec, it has render_views in it.

My problem:

There's no rack.locale key in the request environment. I double-checked rake middlware, it lists Rack::Locale toward the end, right before run MyApp::Application.routes.

After some debugging I found out that the middleware is never called when I run

rspec spec/controllers/authentication_controller_spec.rb

BUT: Running the same code in script/rails s thin gives me more keys in the request env, namely:

rack.request.cookie_string
rack.locale
rack.request.query_string
rack.request.query_hash

So, I guess the question is: Why does RSpec refuse to pick up a Rack middleware?

awendt
  • 13,195
  • 5
  • 48
  • 66

1 Answers1

6

Controller specs do not go through the stack, they pretty much call directly on the controller itself. You'll probably want to use Rspec's request type tests for this.

nowk
  • 32,822
  • 2
  • 35
  • 40