ruby-on-rails - AXLSX:为 rspec 测试解析 xlsx 文件

标签 ruby-on-rails ruby excel rspec axlsx

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

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

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

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

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

有点像...

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

提前致谢!

最佳答案

看起来 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

关于ruby-on-rails - AXLSX:为 rspec 测试解析 xlsx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31951323/

相关文章:

ruby-on-rails - Ruby on Rails : "The change you wanted was rejected" on Form Submission

javascript - 找不到 HotelsController#rooms_availability 的模板,渲染头 :no_content

mysql - Rails 4 - 如何通过Activerecord中的 "joins"和 "includes"命令生成统计信息?

vba - Excel VBA 中的 Like 运算符

c - 将整数与从 VBA 调用的 dll 中的硬编码值进行比较的问题

ruby-on-rails - 如何修复 PG::ProgramLimitExceeded: ERROR?

ruby-on-rails - 具有即用型 Rails 开发环境的 VM 镜像?

ruby - 如何在 Ruby 中定义未命名方法?

Ruby 使用多线程运行两个脚本

excel - 仅使用 VBA 将数据从 Excel 365 上传到 Azure Sql Server