Quantcast
Channel: Ruby – My Programming Blog
Viewing all articles
Browse latest Browse all 12

How to mock file upload with RSpec 3.0 ? [SOLVED]

$
0
0

One more note on RSpec 3.0, this time one useful function that mocks File Upload , saves file/archive content to memory so after it you can do whatever your soul wants with it. Test file/archive should exist in the file system.


def mock_archive_upload(archive_path, type)
  return ActionDispatch::Http::UploadedFile.new(:tempfile => File.new(Rails.root + archive_path , :type => type, :filename => File.basename(File.new(Rails.root + archive_path))))
end

 

e.g of use:


#saves archive into memory so it can be manipulated in tests
mock_archive_upload("../fixtues/my_archive.zip", "application/zip")


Tagged: file_upload, mock archive upload, mock file upload, Rails, Rspec, rspec 3.0, Ruby

Viewing all articles
Browse latest Browse all 12

Trending Articles