gpt4 book ai didi

ruby-on-rails - 如何测试这个 CSV 导入 rake 任务?

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:14 24 4
gpt4 key购买 nike

我不知道从哪里开始测试这个 rake 任务。我需要 stub 吗?如果是这样,如何使用它们?任何帮助,将不胜感激。谢谢!

desc "Import CSV file"
task :import => [:environment] do
data = "db/data.csv"
headers = CSV.open(data, 'r') { |csv| csv.first }
cs = headers[2..-1].map { |c| Model1.where(name: c).first_or_create }
ls = Model2.find_ls

csv_contents = CSV.read(photos)
csv_contents.shift

csv_contents.each do |row|
p = Model2.where(id: row[0], f_name: row[1]).first_or_create
p_d = FastImage.size(p.file.url(:small))
p.update_attributes(dimensions: p_d)

row[2..-1].each_with_index do |ls, i|
unless ls.nil?
ls.split(',').each { |l|
cl = Model3.where(name: l.strip, model_1_id: cs[i].id).first_or_create
Model4.where(p_id: p.id, model_3_id: cl.id).first_or_create
}
end
end
end
end

最佳答案

这是我的做法:

1) 快乐路径测试

这样的 Rake 任务很难测试。将 rake 任务的主体提取到一个类中:

不管怎样.rake

desc "Import CSV file"
task :import => [:environment] do
CSVImporter.new.import "db/data.csv"
end
end

lib/csv_importer.rb

class CsvImporter
def import(data)
headers = CSV.open(data, 'r') { |csv| csv.first }
cs = headers[2..-1].map { |c| Model1.where(name: c).first_or_create }
ls = Model2.find_ls

csv_contents = CSV.read(photos)
csv_contents.shift

csv_contents.each do |row|
p = Model2.where(id: row[0], f_name: row[1]).first_or_create
p_d = FastImage.size(p.file.url(:small))
p.update_attributes(dimensions: p_d)

row[2..-1].each_with_index do |ls, i|
unless ls.nil?
ls.split(',').each { |l|
cl = Model3.where(name: l.strip, model_1_id: cs[i].id).first_or_create
Model4.where(p_id: p.id, model_3_id: cl.id).first_or_create
}
end
end
end
end

现在很容易在测试文件上编写调用 CSVImporter.new.import 的测试(这就是为什么 import 将文件作为参数而不是硬编码)并期待结果。如果在测试环境中导入 db/data.csv 是合理的,您可以根据需要在测试中执行此操作。您可能只需要一个这样的测试。不需要 stub 。

2) 边缘和错误情况

这里有很多逻辑,为了简单和速度,您需要在不创建实际模型对象的情况下进行测试。也就是说,是的,你会想要 stub 。 Model2.find_lsFastImage.size 已经很容易 stub 了。让我们提取一个方法来使其他模型调用易于 stub :

class CsvImporter
def import(data)
headers = CSV.open(data, 'r') { |csv| csv.first }
cs = headers[2..-1].map { |c| Model1.first_or_create_with(name: c) }
ls = Model2.find_ls

csv_contents = CSV.read(photos)
csv_contents.shift

csv_contents.each do |row|
p = Model2.first_or_create_with(id: row[0], f_name: row[1])
p_d = FastImage.size(p.file.url(:small))
p.update_attributes(dimensions: p_d)

row[2..-1].each_with_index do |ls, i|
unless ls.nil?
ls.split(',').each { |l|
cl = Model3.first_or_create_with(name: l.strip, model_1_id: cs[i].id)
Model4.first_or_create_with(p_id: p.id, model_3_id: cl.id)
}
end
end
end
end

app/models/concerns/active_record_extensions.rb

module ActiveRecordExtensions
def first_or_create_with(attributes)
where(attributes).first_or_create
end
end

并将该模块包含在所有需要它的模型中。

现在很容易对所有模型方法进行 stub ,因此您可以编写测试来模拟您喜欢的任何数据库情况。

关于ruby-on-rails - 如何测试这个 CSV 导入 rake 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com