Skip to content

Instantly share code, notes, and snippets.

@Hermanverschooten
Created December 22, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermanverschooten/94ae28f7a276226f17b3 to your computer and use it in GitHub Desktop.
Save Hermanverschooten/94ae28f7a276226f17b3 to your computer and use it in GitHub Desktop.
Class that allows testing of file uploads without an existing file.
require 'rails_helper'
describe DocumentsController do
it 'uploads a file' do
post :create, document: {
name: 'test',
doc: Rack::Test::UploadedFileData.new(
"some content",
"test.txt",
"text/plain"
)
}
expect(Document.count).to eq(1)
end
end
# This goes into spec/support
require "tempfile"
module Rack
module Test
class UploadedFileData < UploadedFile
attr_reader :tempfile
def initialize(data,filename = "data", content_type ="text/plain", binary=false)
@content_type = content_type
@original_filename = filename
@tempfile = Tempfile.new(filename)
@tempfile.set_encoding("Binary") if @tempfile.respond_to?(:set_encoding)
@tempfile.binmode if binary
@tempfile.write(data)
@tempfile.rewind
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment