title: How to stub env['warden'].user for ApplicationCable::Connection tests with rspec
body: <p>Rails 5.2
  I have the following ApplicationCable::Connection ruby file:</p>
  
  <pre><code>module ApplicationCable
    class Connection &lt; ActionCable::Connection::Base
  
      identified_by :current_user
  
      def connect
        self.current_user = find_verified_user
      end
  
      private
  
      def find_verified_user
        if verified_user = env['warden'].user
          verified_user
        else
          message = "The user is not found. Connection rejected."
          logger.add_tags 'ActionCable', message  
          self.transmit error: message 
          reject_unauthorized_connection
        end
      end
    end
  end
  </code></pre>
  
  <p>I want to test this setup and and using the following RSpec test:</p>
  
  <pre><code>require 'rails_helper.rb'
  
  RSpec.describe ApplicationCable::Connection, type: :channel do
  
    it "successfully connects" do
      connect "/cable", headers: { "X-USER-ID" =&gt; 325 }
      expect(connection.user_id).to eq 325
    end
  end
  </code></pre>
  
  <p>Which fails with:</p>
  
  <p>Failure/Error: if verified_user = env['warden'].user</p>
  
  <p>NoMethodError:
     undefined method `[]' for nil:NilClass</p>
  
  <p>So I want to stub out the env['warden'].user code and return an id of 325. 
  I tried the following:</p>
  
  <pre><code>allow(env['warden']).to receive(:user).and_return(325)
  </code></pre>
  
  <p>But that produced the following error:</p>
  
  <p><code>undefined local variable or method</code>env' </p>
  
  <p>How can I test this class?</p>