title: Rspec testing devise failing because assigns always nil
body: <p>I'm having controller test with devise, but it always fail because assigns always return nil, please help to find where the problem is, thanks a million!</p>
  
  <p>posts_controller_spec.rb:</p>
  
  <pre><code>RSpec.describe PostsController, :type =&gt; :controller do
     describe "with valid session" do
        describe "GET index" do
           it "assigns all posts as @posts" do
              sign_in :admin, @user
              post = create(:post)
              get :index, {}
              expect(assigns(:posts)).to eq([post])
           end
        end
     end
     ...
  end
  </code></pre>
  
  <p>posts_controller.rb</p>
  
  <pre><code>class PostsController &lt; ApplicationController
  
     before_action :authenticate_user!
     before_action :set_post, only: [:show, :edit, :update, :destroy]
  
     # GET /posts
     # GET /posts.json
     def index
       @posts = Post.all
     end
     ...
  end
  </code></pre>
  
  <p>I've included devise test helpers in spec/rails_helper.rb</p>
  
  <pre><code>config.include Devise::TestHelpers, type: :controller
  </code></pre>
  
  <p>In my case, post is scoped under admin, not sure if that makes difference (functional test doesn't get through routes?), so I just include my routes.rb here</p>
  
  <p>routes.rb:</p>
  
  <pre><code>Rails.application.routes.draw do
     root to: 'home#index'
     get 'admin', to: 'admin#index'
     devise_for :users
     scope '/admin' do
       resources :posts
     end
  end
  </code></pre>
  
  <p>And finally, the output from rspec: </p>
  
  <pre><code>1) PostsController with valid session GET index assigns all posts as @posts
     Failure/Error: expect(assigns(:posts)).to eq([post])
  
     expected: [#&lt;Post id: 57, title: "MyText", body: "MyText", image_url: "MyString", created_at: "2014-09-02 14:36:01", updated_at: "2014-09-02 14:36:01", user_id: 1&gt;]
          got: nil
  
     (compared using ==)
   # ./spec/controllers/posts_controller_spec.rb:53:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>I've read this thread <a href="https://stackoverflow.com/questions/18384268/rspec-test-of-my-controller-returns-nil-factory-girl">rspec test of my controller returns nil (+factory girl)</a> , and followed the suggestion to change <code>get :index</code> to <code>controller.index</code> . The suggestion is that if that passes the test then it's a routing problem. It does pass the test, but I still have no idea where the routing problem is, and why the <code>get :index</code> is not working...</p>