backend_spec.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. require 'rails_helper'
  2. RSpec.describe ObjectManager::Attribute::Validation::Backend do
  3. describe 'backend interface' do
  4. subject do
  5. described_class.new(
  6. record: record,
  7. attribute: attribute
  8. )
  9. end
  10. let(:record) { build(:user) }
  11. let(:attribute) { ::ObjectManager::Attribute.find_by(name: 'firstname') }
  12. it 'has attr_accessor for record' do
  13. expect(subject.record).to eq(record)
  14. end
  15. it 'has attr_accessor for attribute' do
  16. expect(subject.attribute).to eq(attribute)
  17. end
  18. it 'has attr_accessor for value' do
  19. expect(subject.value).to eq(record[attribute.name])
  20. end
  21. it 'has attr_accessor for previous_value' do
  22. record.save!
  23. previous_value = record[attribute.name]
  24. record[attribute.name] = 'changed'
  25. expect(subject.previous_value).to eq(previous_value)
  26. end
  27. describe '.invalid_because_attribute' do
  28. before do
  29. subject.invalid_because_attribute('has value that is ... .')
  30. end
  31. it 'adds Rails validation error' do
  32. expect(record.errors.count).to be(1)
  33. end
  34. it 'uses ObjectManager::Attribute#name as ActiveModel::Errors identifier' do
  35. expect(record.errors.to_h).to have_key(attribute.name.to_sym)
  36. end
  37. end
  38. end
  39. end