backend_spec.rb 1.6 KB

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