2015-04-26 12:48:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
describe FileSizeValidator, lib: true do
|
2015-04-26 12:48:37 +05:30
|
|
|
let(:validator) { FileSizeValidator.new(options) }
|
|
|
|
let(:attachment) { AttachmentUploader.new }
|
|
|
|
let(:note) { create(:note) }
|
|
|
|
|
|
|
|
describe 'options uses an integer' do
|
|
|
|
let(:options) { { maximum: 10, attributes: { attachment: attachment } } }
|
|
|
|
|
|
|
|
it 'attachment exceeds maximum limit' do
|
2015-09-11 14:41:01 +05:30
|
|
|
allow(attachment).to receive(:size) { 100 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).to have_key(:attachment)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment under maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 1 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).not_to have_key(:attachment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'options uses a symbol' do
|
2015-09-11 14:41:01 +05:30
|
|
|
let(:options) do
|
|
|
|
{
|
|
|
|
maximum: :test,
|
|
|
|
attributes: { attachment: attachment }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
before do
|
|
|
|
allow(note).to receive(:test) { 10 }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment exceeds maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 100 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).to have_key(:attachment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment under maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 1 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).not_to have_key(:attachment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|