繁体   English   中英

使用rspec测试导出到excel或csv的最佳方法是什么?

[英]What is the best way to test export to excel or csv using rspec?

我正在使用ruby CSV库从Ruby On Rails应用程序导出报告数据。 我想知道测试这种操作的最佳方法是什么?

提前致谢

嗨,我使用的是具有更快CSV的Ruby 1.9.2,但概念应该类似。 我实际上有一个辅助方法的工厂,但把它们放在一起,以便更容易看到这里。 我也很想知道是否有更好的方法,但这对我有用。

我生成了报告,然后打开它以将其与我的预期结果进行比较。 我想确保文件存在。 您可以在不实际保存文件的情况下执行类似操作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM