生成假CSV以使用rspec进行测试

生成假CSV以使用rspec进行测试,csv,ruby-on-rails-4,testing,rspec,Csv,Ruby On Rails 4,Testing,Rspec,我想测试导入CSV文件的方法。 但我不知道如何生成假CSV文件来测试它。 我尝试了很多我在stack上已经找到的解决方案,但在我的案例中不起作用 以下是csv原始文件: firstname,lastname,home_phone_number,mobile_phone_number,email,address orsay,dup,0154862548,0658965848,orsay.dup@gmail.com,2 rue du pré paris richard,planc,014587859

我想测试导入CSV文件的方法。 但我不知道如何生成假CSV文件来测试它。 我尝试了很多我在stack上已经找到的解决方案,但在我的案例中不起作用

以下是csv原始文件:

firstname,lastname,home_phone_number,mobile_phone_number,email,address
orsay,dup,0154862548,0658965848,orsay.dup@gmail.com,2 rue du pré paris
richard,planc,0145878596,0625147895,richard.planc@gmail.com,45 avenue du general leclerc
person.rb

 def self.import_data(file)
   filename = File.join Rails.root, file

   CSV.foreach(filename, headers: true, col_sep: ',') do  |row|
     firstname, lastname, home_phone_number, mobile_phone_number, email, address = row

     person = Person.find_or_create_by(firstname: row["firstname"], lastname: row['lastname'], address: row['address'] )
     if person.is_former_email?(row['email']) != true
       person.update_attributes({firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], address: row['address'], email: row['email']})
    end
  end
end
人员规格rb:

require "rails_helper"

RSpec.describe Person, :type => :model do


  describe "CSV file is valid" do
   file = #fake file
   it "should read in the csv" do
   end

   it "should have result" do
  end
 end

 describe "import valid data" do
  valid_data_file = #fake file
  it "save new people" do
   Person.delete_all
   expect { Person.import_data(valid_data_file)}.to change{ Person.count }.by(2)
   expect(Person.find_by(lastname: 'dup').email).to eq "orsay.dup@gmail.com"
 end

 it "update with new email" do
 end
end

describe "import invalid data" do
 invalid_data_file = #fake file
 it "should not update with former email" do
 end
 it "should not import twice from CSV" do
 end
 end
end

使用openoffice或excel并在“保存选项”中将文件另存为.csv文件。一个电子表格程序。

我成功地使用了from来实现您生成包含虚假数据的CSV文件的目的

请按照以下步骤使用它:

  • 打开命令行(即在OSX上使用CMD+Space打开Spotlight,然后输入“Terminal”)
  • 通过运行命令
    Gem Install Faked_CSV
    安装伪造的CSV Gem。注意:如果使用RubyonRails项目,请将
    gem'faked_csv'
    添加到您的gem文件中,然后运行
    bundle安装
  • 通过键入Bash Terminal
    Faked_CSV--version
  • 为伪造的CSV Gem创建一个配置文件,并在其中定义如何生成伪造数据。例如,下面的将生成一个包含200行的CSV文件(或编辑到您想要的数量),并为每个字段包含逗号分隔的列。如果字段
    type
    的值以
    faker:
    为前缀,请参阅的“用法”部分以了解示例
  • my_faked_config.csv.json

    {
      "rows": 200,
      "fields": [
        {
          "name": "firstname",
          "type": "faker:name:first_name",
          "inject": ["luke", "dup", "planc"]
        },
        {
          "name": "lastname",
          "type": "faker:name:last_name",
          "inject": ["schoen", "orsay", "richard"]
        },
        {
          "name": "home_phone_number",
          "type": "rand:int",
          "range": [1000000000, 9999999999]
        },
        {
          "name": "mobile_phone_number",
          "type": "rand:int",
          "range": [1000000000, 9999999999]
        },
        {
          "name": "email",
          "type": "faker:internet:email"
        },
        {
          "name": "address",
          "type": "faker:address:street_address",
          "rotate": 200
        }
      ]
    }
    
  • 运行以下命令以使用配置文件my_faked_config.csv.json在名为my_faked_data.csv的当前文件夹中生成一个csv文件,其中包含伪数据
    faked_csv-i my_faked_config.csv.json-o my_faked_data.csv
  • 由于生成的文件在生成后可能不包括每列的相关标签,因此只需手动在my_faked_data.csv的顶部插入以下行即可
    firstname、lastname、home_phone_number、mobile_phone_number、email、address
  • 查看包含伪数据的my_faked_data.csvcsv文件的最终内容,该文件应类似于以下内容:
  • 我的伪造数据.csv

    firstname,lastname,home_phone_number,mobile_phone_number,email,address
    Kyler,Eichmann,8120675609,7804878030,norene@bergnaum.io,56006 Fadel Mission
    Hanna,Barton,9424088332,8720530995,anabel@moengoyette.name,874 Leannon Ways
    Mortimer,Stokes,5645028548,9662617821,moses@kihnlegros.org,566 Wilderman Falls
    Camden,Langworth,2622619338,1951547890,vincenza@gaylordkemmer.info,823 Esmeralda Pike
    Nikolas,Hessel,5476149226,1051193757,jonathon@ziemannnitzsche.name,276 Reinger Parks
    ...
    
  • 使用下面显示的技术修改您的person\u spec.rb单元测试,该技术将传入模拟数据以测试person.rb文件的
    import\u data
    功能
  • 人员规格rb

    require 'rails_helper'
    
    RSpec.describe Person, type: :model do
    
      describe 'Class' do
        subject { Person }
    
        it { should respond_to(:import_data) }
    
        let(:data) { "firstname,lastname,home_phone_number,mobile_phone_number,email,address\r1,Kyler,Eichmann,8120675609,7804878030,norene@bergnaum.io,56006 Fadel Mission" }
    
        describe "#import_data" do
          it "save new people" do
            File.stub(:open).with("filename", {:universal_newline=>false, :headers=>true}) {
              StringIO.new(data)
            }
            Product.import("filename")
            expect(Product.find_by(firstname: 'Kyler').mobile_phone_number).to eq 7804878030
          end
        end
      end
    
    end
    

    注意:我自己使用它生成了一个大的CSV文件,其中包含有意义的假数据,供我使用。我的应用程序允许用户上传包含特定列名的CSV文件,并将其持久化到PostgreSQL数据库,然后在分页表视图中显示数据,并能够使用AJAX进行搜索和排序。

    是的,我已经考虑过了,但我认为有更有效的方法可以做到这一点。