如何为导入csv编写Rspec测试用例

时间:2018-11-14 13:47:42

标签: ruby-on-rails rspec rspec-rails

我对单元测试还很陌生,我想知道当有人导入CSV时如何进行测试?

class ListsController < ApplicationController

  def import
      List.import(params[:file])
      redirect_to lists_path, notice: 'CSV sucessfully imported.'
    rescue StandardError
      redirect_to lists_path, alert: 'Invalid CSV file format.'
  end

end

型号

class List < ApplicationRecord
      def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
          list_hash = row.to_hash # exclude the price field
          list = List.where(id: list_hash['id'])
          if list.count == 1
            list.first.update_attributes(list_hash)
          else
            List.create!(list_hash)
          end # end if !list.nil?
        end # end CSV.foreach
       end # end self.import(file)
end

0 个答案:

没有答案