生成假CSV以使用rspec进行测试
我想测试导入CSV文件的方法。 但我不知道如何生成假CSV文件来测试它。 我尝试了很多我在stack上已经找到的解决方案,但在我的案例中不起作用 以下是csv原始文件:生成假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
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文件的目的 请按照以下步骤使用它:
Gem Install Faked_CSV
安装伪造的CSV Gem。注意:如果使用RubyonRails项目,请将gem'faked_csv'
添加到您的gem文件中,然后运行bundle安装
Faked_CSV--version
type
的值以faker:
为前缀,请参阅的“用法”部分以了解示例{
"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
}
]
}
faked_csv-i my_faked_config.csv.json-o my_faked_data.csv
firstname、lastname、home_phone_number、mobile_phone_number、email、address
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
...
import\u data
功能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进行搜索和排序。是的,我已经考虑过了,但我认为有更有效的方法可以做到这一点。