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.7.pre spec/models/generic_spec.rb
cmor_carousels-0.0.6.pre spec/models/generic_spec.rb
cmor_carousels-0.0.5.pre spec/models/generic_spec.rb
cmor_carousels-0.0.4.pre spec/models/generic_spec.rb
cmor_carousels-0.0.3.pre spec/models/generic_spec.rb
cmor_carousels-0.0.2.pre spec/models/generic_spec.rb
cmor_carousels-0.0.1.pre spec/models/generic_spec.rb