我正在创建功能测试(使用 Rspec/Capybara),并在打开和读取 .xlsx 文件的功能上遇到此问题。
失败/错误:
当 ".xlsx" 然后 Roo::Excelx.new(file.path, nil, :ignore)
否则提出“Tipo de arquivo desconhecido:#{file.original_filename}”
Zip::ZipError:未找到中央目录签名的 Zip 结尾
此错误仅在运行测试时发生。它在本地和生产中完美运行。
我发现此错误的一些可能原因是文件损坏和错误的文件扩展名。
发生错误的方法:
def open_spreadsheet
case File.extname(file.original_filename)
when ".csv" then Csv.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Tipo de arquivo desconhecido: #{file.original_filename}"
end
end
考试:
RSpec.feature "LoadSponsorDatabaseAndInviteUsers", type: :feature do
scenario "loading sponsor database" do
admin = create(:user_admin)
login_as(admin, :scope => :user)
visit(sponsor_database_imports_new_path)
attach_file("sponsor_database_import[file]", Rails.root + "spec/fixtures/test_data.xlsx")
click_button("Load File")
expect(page).to have_content("Some content")
end
end
加载文件时,它应该将我重定向到加载数据的不同页面。
我在这里缺少任何配置吗?
当我测试这种行为(比较测试和开发环境)时,我发现了这一点:
该文件已正确附加到表单。然后,上传时保存为临时文件,但测试过程创建的临时文件已损坏(我无法手动打开它)。