I'm having an issue with testing my controllers and using Warden.
All examples point at stubbing request.env['warden']
. This causes issues in my controllers when I call env['warden']
, which then returns nil
.
For a crude example, using this:
request.env['warden'] = double(Warden, :authenticate => nil,
:authenticate! => nil,
:authenticated? => false)
And a simple before filter like this:
before_filter do
redirect_to new_user_session_url unless env['warden'].authenticated?
end
I get a nil
.
I just managed to get it working using controller.env['warden'] = ...
and it works.
This makes sense, since it's sitting right at the controller level, so I guess my question is what wouldn't it work in the I've seen all examples.
I have this in my spec_helper
:
config.include Warden::Test::Helpers
Any help would be great!
I wrote controller test helpers for Warden.
http://kentaroimai.com/articles/1-controller-test-helpers-for-warden
Despite many examples telling you to implement Warden through env['warden']
in your Rails app. It seems the correct way to access it through request.env['warden']
.
It found this out by raising env in my controllers during tests, and this always came out nil
.
It seems in Warden, https://github.com/hassox/warden/blob/master/lib/warden/proxy.rb#L13 There is an accessor for the rack environment, which won't exist in test mode due to the absence of Rack in controller tests. Please someone check this.
So when stubbing request.env
in RSpec, your implementation needs to point at request.env
.
It seems a necessary evil in my mind. But if there is anyone with a good explanation or work around, I'd love to continue this discussion.
来源:https://stackoverflow.com/questions/9261191/stubbing-warden-on-controller-tests