Skip to content

Instantly share code, notes, and snippets.

@ruedap
Created November 7, 2013 17:50
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 ruedap/7358844 to your computer and use it in GitHub Desktop.
Save ruedap/7358844 to your computer and use it in GitHub Desktop.
class UploadForm
  include ActiveModel::Model

  attr_accessor :upload_file

  validates_each :upload_file do |record, attr, value|
    if value.present? && value.size > 1.megabytes
      record.errors.add(attr, 'ファイルサイズがビッグだね')
    end
  end
end
describe UploadForm do
  describe 'バリデーション' do
    describe 'upload_file' do
      context 'アップロードしたファイルサイズがビッグな場合' do
        before do
          filename = 'invalid.pdf'
          filepath = File.join(Rails.root, "spec/fixtures/files/#{filename}")
          @upload_file = ActionDispatch::Http::UploadedFile.new(
            filename: File.basename(filepath),
            tempfile: File.open(filepath)
          )
        end

        let(:upload_form) { UploadForm.new({ upload_file: @upload_file }) }

        it 'バリデーションエラーが起きること' do
          upload_form.valid?
          expect(upload_form).to have(1).errors_on(:upload_file)
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment