开发者

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

开发者 https://www.devze.com 2023-02-03 09:17 出处:网络
I\'m 开发者_开发百科using ruby CSV library to export reporting data from Ruby On Rails application.

I'm 开发者_开发百科using ruby CSV library to export reporting data from Ruby On Rails application. And i am wondering what would be the best way to test such operation?

Thanks in advance


Hi I was using Ruby 1.9.2 which has Faster CSV, but the concept should be similar. I actually have my factories in a helper method, but put it all together to make it easier to see here. I'd also love to know if there is a better way, but this worked for me.

I generated the report, and then opened it to compare it with my expected results. I wanted to make sure the file existed. You could do something similar without actually saving the file.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号