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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With