0

I am using axlsx gem to write contents to .xlsx file. I am using following code to stream the file to the browser:

  send_data excel_package.to_stream.read, type: "application/xlsx", filename: filename

Now I want to write test cases for this and want to read the same file in my rspec. How can I do that ? I looked for other libraries that can read excel file, but all of those read from the disk.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Zack
  • 105
  • 8
  • Why not just deal with the stream directly e.g. `excel_package.to_stream.read`? This will return a `String` the same as `File#read` would. `to_stream` returns a `StringIO` and `read` will return the full stream as a String – engineersmnky Mar 25 '15 at 18:00
  • excel_package.to_stream.read return an encoded string. I need something to parse it, to make it readable again. Preferably in a way I could easily read the excel file off the disk using libraries like creek, spreadsheet etc – Zack Mar 25 '15 at 22:15

1 Answers1

0

The package is a zip archive.

The simplest thing you can do is write the package to disk and load it with RubyZip, however if you want everything working without disk I/O you might take a look at rubyzip's InputStream to load from StringIO, https://github.com/rubyzip/rubyzip/blob/master/lib/zip/input_stream.rb

Once you have access to the zip archive. From there, you load the entry for the package part you want to spec with tools like Nokogiri and assert as needed.

You will find a sample list of parts here https://github.com/randym/axlsx/blob/master/test/tc_package.rb#L163

randym
  • 2,430
  • 1
  • 19
  • 18