lines_validator_spec.rb 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe LinesValidator do
  4. let(:record_class) do
  5. Class.new do
  6. include ActiveModel::Validations
  7. attr_accessor :text
  8. validates :text, lines: { maximum: 5 }
  9. end
  10. end
  11. let(:record) { record_class.new }
  12. describe '#validate_each' do
  13. context 'with a nil value' do
  14. it 'does not add errors' do
  15. record.text = nil
  16. expect(record).to be_valid
  17. expect(record.errors).to be_empty
  18. end
  19. end
  20. context 'with lines below the limit' do
  21. it 'does not add errors' do
  22. record.text = "hoge\n" * 5
  23. expect(record).to be_valid
  24. expect(record.errors).to be_empty
  25. end
  26. end
  27. context 'with more lines than limit' do
  28. it 'adds an error' do
  29. record.text = "hoge\n" * 6
  30. expect(record).to_not be_valid
  31. expect(record.errors.where(:text)).to_not be_empty
  32. end
  33. end
  34. end
  35. end