title: How to sign in for a Rspec request with new_user_session post?
body: <p>I am writing a very simple test: is my page displayed when my users is logged in? I use a Factory to create the test user, and request to check the case:</p>
  
  <pre><code>FactoryBot.define do
    sequence(:login) {|n| "user#{n}"}
    sequence(:email) {|n| "user#{n}@gmail.com"}
    factory :user do
      playground_id       {0}
      default_playground_id {0}
      group_id            {0}
      first_name          {"user"}
      last_name           {"test"}
      user_name           {FactoryBot.generate :login}
      email               {FactoryBot.generate :email}
      password            {"DQtest_user01"}
      password_confirmation {"DQtest_user01"}
      description         {"This is a test User used for unit testing"}
      created_by          {"Rspec"}
      updated_by          {"Rspec"}
      active_from         {"2010.01.01"}
      active_to           {"2100.01.01"}
      confirmed_at        {Time.now}
    end
  end
  </code></pre>
  
  <p>If logged in, the index template should be displayed, otherwise we are redirected to the sign_in form (based on Devise)</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe BusinessArea, type: :request do
  
    describe "Business Areas pages: " do
      context "when not signed in " do
        it "should propose to log in" do
          get "/business_areas/index"
          follow_redirect!
          expect(response.body).to include('Sign in')
        end
      end
      context "when signed in" do
        before do
          get "/users/sign_in"
          test_user = FactoryBot.create(:user)
          post "/users/sign_in", :params =&gt; { user_login: test_user.user_name, user_password: test_user.password}
        end
        it "should display index" do
          get "/business_areas/index"
          expect(response).to render_template(:index)
        end
      end
    end
  end
  </code></pre>
  
  <p>The form generated for login provides the following fields to Devise:</p>
  
  <pre><code>      &lt;label for="user_login"&gt;Login&lt;/label&gt;&lt;br /&gt;
        &lt;input autofocus="autofocus" type="text" name="user[login]" id="user_login" /&gt;
        &lt;label for="user_password"&gt;Password&lt;/label&gt;&lt;br /&gt;
        &lt;input autocomplete="off" type="password" name="user[password]" id="user_password" /&gt;
  </code></pre>
  
  <p>Running the test returns this message:</p>
  
  <blockquote>
    <p>Failures:</p>
    
    <p>1) BusinessArea Business Areas pages:  when signed in should display
    index
         Failure/Error: expect(response).to render_template(:index)
           expecting &lt;"index"> but was a redirect to <a href="http://www.example.com/users/sign_in" rel="nofollow noreferrer">http://www.example.com/users/sign_in</a>
         # ./spec/requests/business_areas_spec.rb:21:in `block (4 levels) in '</p>
    
    <p>Finished in 0.17126 seconds (files took 2.24 seconds to load) 2
    examples, 1 failure</p>
  </blockquote>
  
  <p>It looks rather simple, but I don't get the point: why doesn't it log in?
  Or is it another issue? Is the user actually created, how can I be sure FactoryBot is successful?</p>
  
  <p>Thanks for your help.</p>