LoginSignup
6
7

More than 5 years have passed since last update.

Rails MiniTestでCSVファイルを返すControllerをテストしたい

Last updated at Posted at 2014-05-02

Rails MiniTestでCSVファイルを返すControllerをテストしたい

CSVデータファイルを返すControllerがあり、
そのCSVデータの中身を次のようにテストしました。

Controller側

def create_csv
  <>
  respond_to do |format|
    format.csv {
      send_data records.to_csv, :type => 'text/csv', :filename => 'test.csv'
    return
    }
  end
end

TestCode側
(次の例では各行が'数字,数字,数字'になっていることを確認)

describe "create_csv" do
  it "return csv file" do
    get :create_csv
    response.body.each_line |line| do
      assert_match /\d,\d,\d/, line
    end
  end
end
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7