Sha256: fe59ad352f654ce9eac8f2160c955600eb65298a8f798afb0701d4c6912291dc

Contents?: true

Size: 1.86 KB

Versions: 42

Compression:

Stored size: 1.86 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 ] },
  }.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

42 entries across 42 versions & 2 rubygems

Version Path
cmor_system_backend-0.0.43.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.42.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.41.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.40.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.39.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.38.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.37.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.36.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.35.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.34.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.33.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.32.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.31.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.30.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.29.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.28.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.27.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.26.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.25.pre spec/models/generic_spec.rb
cmor_system_backend-0.0.24.pre spec/models/generic_spec.rb