6

知道如何为从 xlsx gem 生成的 xlsx 文件编写视图规范(是否存在标题、行等)?不确定我是否一开始就做对了,但这就是我到目前为止所拥有的

RSpec.describe "spreadsheet.xlsx.axlsx", :type =>  :view do
    ...
    it "should have header Books" do
      assign(:spreadsheet, spreadsheet)
      render
      # rendered.rows[0].cells.map(&:value).should include "Books"
    end
end

在撬,rendered是在一个 utf-8 编码的字符串中,我不确定如何解析标头等。

=> "PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!\xECc8k\xD4\

有没有一种方法可以像测试 html 视图一样测试生成的 xlsx 文件?

就像是...

it "has header Books" do
  assign(:worksheet, worksheet)
  render
  expect(rendered).to have_xpath("(//table)[1]/thead/tr/td", :text => "Books")
end

提前致谢!

4

1 回答 1

8

It appears rendered is the raw response so you can use something like the axlsx_rails request specs:

File.open('/tmp/xlsx_temp.xlsx', 'w') {|f| f.write(rendered) }
wb = nil
expect{ wb = Roo::Excelx.new('/tmp/xlsx_temp.xlsx') }.to_not raise_error
wb.cell(2,1).should == 'Some value'

This uses the roo gem to parse the file since Axlsx does not read xlsx.

See: https://github.com/straydogstudio/axlsx_rails/blob/master/spec/axlsx_request_spec.rb#L19-L22

于 2015-08-12T21:45:35.257 回答