Sha256: 49c4b076e9e1f879526d14c5953c3351ab4eb547b4d6f1cd0bfb718461ebf591

Contents?: true

Size: 1.64 KB

Versions: 47

Compression:

Stored size: 1.64 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
  ]

  {
    Cmor::Carousels::Carousel   => {},
    Cmor::Carousels::ItemDetail => {},
  }.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

47 entries across 47 versions & 1 rubygems

Version Path
cmor_carousels-0.0.29.pre spec/models/generic_spec.rb
cmor_carousels-0.0.28.pre spec/models/generic_spec.rb
cmor_carousels-0.0.27.pre spec/models/generic_spec.rb
cmor_carousels-0.0.26.pre spec/models/generic_spec.rb
cmor_carousels-0.0.25.pre spec/models/generic_spec.rb
cmor_carousels-0.0.24.pre spec/models/generic_spec.rb
cmor_carousels-0.0.22.pre spec/models/generic_spec.rb
cmor_carousels-0.0.21.pre spec/models/generic_spec.rb
cmor_carousels-0.0.20.pre spec/models/generic_spec.rb
cmor_carousels-0.0.19.pre spec/models/generic_spec.rb
cmor_carousels-0.0.18.pre spec/models/generic_spec.rb
cmor_carousels-0.0.17.pre spec/models/generic_spec.rb
cmor_carousels-0.0.15.pre spec/models/generic_spec.rb
cmor_carousels-0.0.14.pre spec/models/generic_spec.rb
cmor_carousels-0.0.13.pre spec/models/generic_spec.rb
cmor_carousels-0.0.12.pre spec/models/generic_spec.rb
cmor_carousels-0.0.11.pre spec/models/generic_spec.rb
cmor_carousels-0.0.10.pre spec/models/generic_spec.rb
cmor_carousels-0.0.9.pre spec/models/generic_spec.rb
cmor_carousels-0.0.8.pre spec/models/generic_spec.rb