The model must be created before to attach a file.
Last active
August 29, 2015 14:20
-
-
Save gpr/8ea2a4720a2fe13891c8 to your computer and use it in GitHub Desktop.
Paperclip warning
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
class MyModel < ActiveRecord::Base | |
has_attached_file :attachment, processors: [:text] | |
validates_attachment_content_type :attachment, content_type: 'text/plain' | |
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
require 'test_helper' | |
require 'faker' | |
describe MyModel do | |
it 'fails to store the attachment at the right location' do | |
file = Tempfile.new(['test_file', '.txt'], '/tmp') | |
file.write(Faker::Lorem.sentence) | |
file.close | |
mm = MyModel.create(attachment: File.open(file)) | |
File.exist?(mm.attachment.path).must_equal true | |
file.unlink | |
end | |
it 'successfully stores the attachment at the right location' do | |
file = Tempfile.new(['test_file', '.txt'], '/tmp') | |
file.write(Faker::Lorem.sentence) | |
file.close | |
mm = MyModel.create(name: 'test') | |
mm.attachment = File.open(file) | |
mm.save | |
File.exist?(mm.attachment.path).must_equal true | |
file.unlink | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment