object_lookup_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ObjectLookup, type: :model do
  4. describe '.by_name' do
  5. context 'with name matching an existing ObjectLookup record' do
  6. subject(:object_lookup) { create(:object_lookup) }
  7. it 'returns its id' do
  8. expect(described_class.by_name(object_lookup.name))
  9. .to eq(object_lookup.id)
  10. end
  11. end
  12. context 'with name not matching any ObjectLookup records' do
  13. let(:name) { 'FooBar' }
  14. it 'creates a new one with that name' do
  15. expect { described_class.by_name(name) }
  16. .to change(described_class, :count).by(1)
  17. expect(described_class.last.name).to eq(name)
  18. end
  19. it 'returns its id' do
  20. expect(described_class.by_name(name))
  21. .to eq(described_class.last.id)
  22. end
  23. context 'for names not in strict CamelCase' do
  24. let(:name) { 'Foo_Bar' }
  25. it 'does not modify the format' do
  26. described_class.by_name(name)
  27. expect(described_class.last.name).to eq(name)
  28. end
  29. end
  30. end
  31. end
  32. describe '.by_id' do
  33. context 'with number matching an existing ObjectLookup#id' do
  34. subject(:object_lookup) { create(:object_lookup) }
  35. it 'returns its name' do
  36. expect(described_class.by_id(object_lookup.id))
  37. .to eq(object_lookup.name)
  38. end
  39. end
  40. end
  41. end