Sha256: 78bf3623cf1faaa882cd051e8abf5368df2dcbe19d34384078a6e47c83ac61bb

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

require 'rails_helper'

RSpec.describe 'ActiveRecord::Base models', type: :model do
  DEFAULT_SPECS_TO_RUN = [
    :is_an_active_record,
    :is_instanciable,
    :valid_with_correct_attributes,
    :not_valid_with_empty_attributes,
    :saves_with_valid_attributes
  ]

  {
    ActiveStorage::Attachment           => { specs_to_skip: [ :not_valid_with_empty_attributes ] },
    ActiveStorage::Blob                 => { specs_to_skip: [ :not_valid_with_empty_attributes ] },
    Delayed::Backend::ActiveRecord::Job => { specs_to_skip: [ :not_valid_with_empty_attributes ] },
    Cmor::System::Changelog             => { specs_to_skip: [ :is_an_active_record, :saves_with_valid_attributes ] },
  }.each do |model, options|
    options.reverse_merge!(specs_to_run: DEFAULT_SPECS_TO_RUN, specs_to_skip: [])
    specs_to_run = options.delete(:specs_to_run)
    specs_to_skip = options.delete(:specs_to_skip)
    specs = specs_to_run - specs_to_skip
    
    describe model do
      it 'is an ActiveRecord::Base' do
        expect(ActiveRecord::Base.descendants).to include(model)
      end if specs.include?(:is_an_active_record)

      it 'is instanciable' do
        instance = model.new
        expect(instance).to be_a(model)
      end if specs.include?(:is_instanciable)

      it 'is valid with correct attribute values' do
        instance = build(model.to_s.tableize.singularize.underscore.tr('/', '_'))
        
        instance.valid?
        expect(instance.errors.full_messages).to eq([])
      end if specs.include?(:valid_with_correct_attributes)

      it 'is not valid with empty attributes' do
        instance = model.new
        expect(instance).not_to be_valid
      end if specs.include?(:not_valid_with_empty_attributes)

      it 'saves with valid attributes' do
        instance = build(model.to_s.tableize.singularize.underscore.tr('/', '_'))
        expect(instance.save).to be_truthy
        expect(instance).to be_persisted
      end if specs.include?(:saves_with_valid_attributes)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cmor_system-0.0.51.pre spec/models/generic_spec.rb
cmor_system-0.0.50.pre spec/models/generic_spec.rb
cmor_system-0.0.49.pre spec/models/generic_spec.rb
cmor_system-0.0.48.pre spec/models/generic_spec.rb
cmor_system-0.0.45.pre spec/models/generic_spec.rb
cmor_system-0.0.44.pre spec/models/generic_spec.rb