title: rspec to test respond_to format and csv
body: <p>I want to test if top_users eq User.top_users in my rspec controller. 
  How do i access the format.csv? I need something like:</p>
  
  <pre class="lang-rb prettyprint-override"><code>it "format csv" do
    get :index, format: :csv
    # expect(something)……
  end
  </code></pre>
  
  <p>Later want to test the csv: if file format is correct, without saving/downloading it.</p>
  
  <p>Controller:</p>
  
  <pre class="lang-rb prettyprint-override"><code>def index
    respond_to do |format|  
      format.csv do
        top_users = User.top_users
  
        send_data(
          top_users.to_csv,
          filename: "top-users-#{Time.zone.today}.csv"
        )
      end
    end
  end
  </code></pre>
  
  <p>Model:</p>
  
  <pre class="lang-rb prettyprint-override"><code>def self.to_csv
    CSV.generate(headers: true) do |csv|
      csv &lt;&lt; [‘one’, ‘two’]
  
      all.each do |user|
        csv &lt;&lt; user.csv_data
      end
    end
  end
  </code></pre>
  
  <p>csv_data is: [user.name, user.email] or so…</p>