【问题标题】:AXLSX: Parsing xlsx file for rspec testsAXLSX:为 rspec 测试解析 xlsx 文件
【发布时间】:2015-08-11 20:25:52
【问题描述】:

知道如何为从 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

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby excel rspec axlsx


    【解决方案1】:

    看来rendered 是原始响应,因此您可以使用类似axlsx_rails 的请求规范:

    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'
    

    这使用roo gem 来解析文件,因为Axlsx 不读取xlsx

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

    【讨论】:

    • 这很完美,正是我所需要的。谢谢!
    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2017-09-23
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多