Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't think that this question applied to my situation, but it does: <a href="https://stackoverflow.com/questions/9261191/stubbing-warden-on-controller-tests">Stubbing Warden on Controller Tests</a></p> <p>As it turns out, Warden does not get included into RSpec controller specs, so you need to do some magic to finagle it in.</p> <p><a href="https://stackoverflow.com/users/1222666/kentaro-imai">Kentaro Imai</a>'s <a href="http://kentaroimai.com/articles/1-controller-test-helpers-for-warden" rel="nofollow noreferrer">Controller test helpers for Warden</a> blog post was particularly helpful. Here's how I got it working for RSpec.</p> <p><strong>Step 1:</strong> Create <code>spec/spec_helper/warden.rb</code> and paste in these contents, which Kentaro derived from Devise:</p> <pre><code>module Warden # Warden::Test::ControllerHelpers provides a facility to test controllers in isolation # Most of the code was extracted from Devise's Devise::TestHelpers. module Test module ControllerHelpers def self.included(base) base.class_eval do setup :setup_controller_for_warden, :warden if respond_to?(:setup) end end # Override process to consider warden. def process(*) # Make sure we always return @response, a la ActionController::TestCase::Behavior#process, even if warden interrupts _catch_warden {super} || @response end # We need to setup the environment variables and the response in the controller def setup_controller_for_warden @request.env['action_controller.instance'] = @controller end # Quick access to Warden::Proxy. def warden @warden ||= begin manager = Warden::Manager.new(nil, &amp;Rails.application.config.middleware.detect{|m| m.name == 'Warden::Manager'}.block) @request.env['warden'] = Warden::Proxy.new(@request.env, manager) end end protected # Catch warden continuations and handle like the middleware would. # Returns nil when interrupted, otherwise the normal result of the block. def _catch_warden(&amp;block) result = catch(:warden, &amp;block) if result.is_a?(Hash) &amp;&amp; !warden.custom_failure? &amp;&amp; !@controller.send(:performed?) result[:action] ||= :unauthenticated env = @controller.request.env env['PATH_INFO'] = "/#{result[:action]}" env['warden.options'] = result Warden::Manager._run_callbacks(:before_failure, env, result) status, headers, body = warden.config[:failure_app].call(env).to_a @controller.send :render, :status =&gt; status, :text =&gt; body, :content_type =&gt; headers['Content-Type'], :location =&gt; headers['Location'] nil else result end end end end end </code></pre> <p><strong>Step 2:</strong> In <code>spec/spec_helper.rb</code>, within the <code>RSpec.configure</code> block, add this line to include the new module:</p> <pre><code>config.include Warden::Test::ControllerHelpers, type: :controller </code></pre> <p><strong>Step 3:</strong> To log in a user in a <code>before</code> block, use syntax similar to this:</p> <pre><code>before { warden.set_user FactoryGirl.create(:user) } </code></pre> <p><strong>Step 4:</strong> Make sure that you reference <code>request.env['warden']</code> in your controllers, not <code>env['warden']</code>. The latter will not work in controller specs in the <code>test</code> environment.</p> <p>Hat tip to Kentaro Imai, whom I owe a beer one day (or in another life)!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload