title: How to access session data from test?
body: <p>The <a href="http://mojolicious.org/" rel="nofollow noreferrer">Mojolicious</a> framework <a href="https://mojolicious.org/perldoc/Mojolicious/Guides/Testing#Testing-application-helpers" rel="nofollow noreferrer">states</a> next:</p>
  
  <blockquote>
    <p>Any aspect of the application (helpers, plugins, routes, etc.) can be introspected from Test::Mojo through the application object.</p>
  </blockquote>
  
  <p>But when helper, for example, <code>$c-&gt;current_user</code> deals with session it fails.</p>
  
  <p>The session data is not available and I can not access it from test:</p>
  
  <pre><code>$t-&gt;app-&gt;session  # {}
  </code></pre>
  
  <p>Thus <code>$t-&gt;app-&gt;current_user</code> fails too.</p>
  
  <p>How to access session data from test?</p>
  
  <p><strong>UPD</strong> The test</p>
  
  <pre><code>use Mojo::Base -strict;
  
  use Mojolicious::Lite;
  
  use Test::More;
  use Test::Mojo;
  
  get '/set_session' =&gt; sub {
      my $c =  shift;
      $c-&gt;session-&gt;{ user_id } = 1;
      $c-&gt;render( text =&gt; $c-&gt;session-&gt;{ user_id } );
  };
  
  get '/get_session' =&gt; sub {
      my $c =  shift;
      $c-&gt;render( text =&gt; $c-&gt;session-&gt;{ user_id } );
  };
  
  my $t = Test::Mojo-&gt;new;
  $t-&gt;get_ok( '/set_session' )-&gt;status_is(200);
  
  is $t-&gt;app-&gt;session-&gt;{ user_id }, 1, 'Session available from test script';
  
  $t-&gt;get_ok( '/get_session' )-&gt;status_is(200)
    -&gt;content_is( 1 );
  
  done_testing();
  </code></pre>
  
  <p><strong>UPD</strong> test result</p>
  
  <pre><code>ok 1 - GET /set_session
  ok 2 - 200 OK
  not ok 3 - Session available from test script
  #   Failed test 'Session available from test script'
  #   at t/session.t line 22.
  #          got: undef
  #     expected: '1'
  ok 4 - GET /get_session
  ok 5 - 200 OK
  ok 6 - exact match for content
  1..6
  # Looks like you failed 1 test of 6.
  </code></pre>
  
  <p><strong>UPD</strong></p>
  
  <p>It seems that <code>Mojo::Test</code> object should save session object in addition to <a href="https://mojolicious.org/perldoc/Mojolicious/Guides/Testing#Test::Mojo-at-a-glance" rel="nofollow noreferrer"><code>the request and response objects from the previous transaction</code></a></p>