Zavitoski
Zavitoski

Reputation: 403

Temp File Corruption while loading .xlsx file on rails test environment

I'm creating feature tests (using Rspec/Capybara), and run into this problem on a feature that opens and reads a .xlsx file.

Failure/Error:

when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)

else raise "Tipo de arquivo desconhecido: #{file.original_filename}"

Zip::ZipError: Zip end of central directory signature not found

This error happens only when running tests. It runs perfectly locally and in production.

I have found that some probable causes for this error are file corruption and wrong file extention.

The method where the error occurs:

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

The test:

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

As the file is loaded, it should redirect me to a different page with the loaded data.

Is there any configuration I'm missing here?


As I was testing this behavior (comparing the test and development enviroments), I found out this:

The file is correctly attached to the form. Then, when uploaded, it is saved as a Temp File, but the temp file created by the test process is corrupted (I couldn't manually open it).

Upvotes: 1

Views: 354

Answers (0)

Related Questions