Created
December 22, 2015 14:52
-
-
Save Hermanverschooten/94ae28f7a276226f17b3 to your computer and use it in GitHub Desktop.
Class that allows testing of file uploads without an existing file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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