我正在使用 ruby CSV 库从 Ruby On Rails 应用程序导出报告数据。 我想知道测试此类操作的最佳方法是什么?
提前致谢
最佳答案
您好,我使用的是 Ruby 1.9.2,它具有更快的 CSV,但概念应该相似。实际上,我的工厂采用辅助方法,但将它们放在一起以便于在此处查看。我也很想知道是否有更好的方法,但这对我有用。
我生成了报告,然后打开它与我的预期结果进行比较。我想确保该文件存在。您可以在不实际保存文件的情况下执行类似的操作。
describe "Rsvp Class" do
created = "#{Time.now.to_formatted_s(:db)} -0800"
before(:each) do
[{:first_name => "fred", :last_name => "flintstone", :state => "CA", :zip => "12345", :created_at => created},
{:first_name => "barny", :last_name => "rubble", :state => "CA", :zip => "12345", :created_at => created}].each do |person|
rsvp = Factory.build(:rsvp)
rsvp.first_name = person[:first_name]
rsvp.last_name = person[:last_name]
rsvp.state = person[:state]
rsvp.zip = person[:zip]
rsvp.created_at = person[:created_at]
rsvp.save
end
end
it "should generate a report csv file" do
response = "first name, last name, email, address, address line 1, city, state, zip, variation code, tracking code, created at\nfred, flintstone, fred@example.com, , , , CA, 12345, , , #{created}\nbarny, rubble, fred@example.com, , , , CA, 12345, , , #{created}\n"
Rsvp.generate_report
string = ""
CSV.foreach("#{::Rails.root.to_s}/reports/rsvp_report.csv", :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
string << "#{row.join(", ")}\n"
end
string.should == response
end
end
关于ruby-on-rails - 使用 rspec 测试导出到 excel 或 csv 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617405/