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