title: rspec test GET #index - expected collection contained
body: <p>I test., that index action 'populates an array of all questions.</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe QuestionsController, type: :controller do
    describe 'GET #index' do
      before do
        @questions = FactoryGirl.create_list(:question, 2)
        get :index
      end  
  
      it 'populates an array of all questions' do
        binding.pry
        expect(assigns(:questions)).to match_array(@questions)  
      end
      it 'renders index view' do
        expect(response).to render_template(:index)  
      end
    end
  end 
  </code></pre>
  
  <p>controllers/questions_controller</p>
  
  <pre><code>class QuestionsController &lt; ApplicationController
      def index
        @questions = Question.all        
      end
  end
  </code></pre>
  
  <p>factories/questions.rb</p>
  
  <pre><code>FactoryGirl.define do
    factory :question do
      title "MyString"
      body "MyText"
    end
  end
  </code></pre>
  
  <p>When running tests display error: </p>
  
  <p>1) QuestionsController GET #index populates an array of all questions
       Failure/Error: expect(assigns(:questions)).to match_array(@questions)</p>
  
  <pre><code>   expected collection contained:  
  
  [#&lt;Question id: 37, title: "MyString", body: "MyText", created_at: "2016-10-31 19:37:12", updated_at:...: "MyString", body: "MyText", created_at: "2016-10-31 19:37:12", updated_at: "2016-10-31 19:37:12"&gt;]
         actual collection contained:    [#&lt;Question id: 15, title: "MyString", body: "MyText", created_at: "2016-10-30 21:23:52", updated_at:...: "MyString", body: "MyText", created_at: "2016-10-31 19:37:12", updated_at: "2016-10-31 19:37:12"&gt;]
         the extra elements were:        [#&lt;Question id: 15, title: "MyString", body: "MyText", created_at: "2016-10-30 21:23:52", updated_at:...: "MyString", body: "MyText", created_at: "2016-10-30 21:23:52", updated_at: "2016-10-30 21:23:52"&gt;]
   # ./spec/controllers/questions_controller_spec.rb:12:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>Why not the same elements of the collection?</p>