Reputation: 179
I have written rspec for controller. And paperclip is used for file upload. I have supposed to write rspec for pagination. For that I want create multiple entries, each entry should have file with csv extension and each file name should be unique.
For that I have move on to Faker gem for generating fake files.
I have tried like
30.times { Post.create!(file: Rack::Test::UploadedFile.new(Faker::File.file_name(dir: Rails.root + 'spec/fixtures/file', name: "testing_file_#{rand(100)}", ext: 'csv'))) }
But It will raise an error like
RuntimeError: /Users/*/**/spec/fixtures/file/testing_file_28.csv file does not exist
I have no idea, about FAKER::FILE. Is there is any way to succeed this approach.
Upvotes: 1
Views: 1623
Reputation: 83
Does the file testing_file_28.csv
really exist in that path? AFAIK, Faker does not generate files, but will just load one that you do have in your directory (source: https://github.com/faker-ruby/faker/blob/master/doc/default/file.md)
Why do you need Faker
for that? Would
Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/files/testing_file_#{rand(100)}.csv", 'application/csv')
also do the job?
consider using Rails.root.join('foo', 'bar')
over Rails.root + "foo/bar"
to avoid troubles with path separators (i.e. /
vs. \
depending on your OS). If you go with Faker, and you pass the separator manually, consider using File::Separator
from the Ruby kernel, see: https://ruby-doc.org/core-2.7.0/File.html
Since it is a CSV file, you could also write a method, that just generates random CSVs, using CSV.generate
. CSV, in the end, is just a string with ,
and ;
/\n
as separators. This you can just pass as IO object to Rack::Test::UploadedFile.new
, i.e. no need for physical files bloating your spec folder.
Upvotes: 1