object_lookup_spec.rb 1.3 KB

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