Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload testing using rspec

How do I generate a file for testing with a given size and test file upload in rspec? I want to generate the file.

I currently have the files in fixtures/files and use fixture_file_upload as a test file. I want to eliminate the need for that sample file.

like image 438
PiKaY Avatar asked Nov 01 '22 23:11

PiKaY


1 Answers

You could use factory_girl or fabrication to mock the uploaded file.

A sample configuration for factory_girl would look like:

factory :attachment do
  supporting_documentation_file_name { 'test.pdf' }
  supporting_documentation_content_type { 'application/pdf' }
  supporting_documentation_file_size { 1024 }
end

Take a look in this nice article on why and how to do that using factory girl

This, as the article describes, requires a shift in your testing philosophy. Instead of testing the file uploading itself (which the gem you are using is already testing for you) you just focus on the necessary models.

An alternative to exercise the actual uploading can be found here Mocking file uploads in Rails 3.1 controller tests

like image 92
Kostas Rousis Avatar answered Nov 13 '22 21:11

Kostas Rousis