backend_spec.rb 1.4 KB

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