Sha256: cebfc5cabb38d3a0a3e19df11a43062ce37eb54f9a436646b096bf3b8483fb4d

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true
describe Grape::Roar::Extensions::Relations::Validations::ActiveRecord, active_record: true do
  let(:model_klass) { double }

  subject do
    klass = Class.new do
      def initialize(klass)
        @klass = klass
      end

      attr_reader :klass
    end

    klass.include(described_class)
    klass.new(model_klass)
  end

  before do
    expect(model_klass).to receive(:reflections).twice
                                                .and_return(reflections)
  end

  %w[
    belongs_to
    has_many
    has_one
    has_and_belongs_to_many
  ].each do |relation|
    context "##{relation}_valid?" do
      let(:relation_klass) do
        "::ActiveRecord::Reflection::#{relation.camelize}"\
          'Reflection'.constantize.allocate
      end

      let(:reflections) { { test: relation_klass, fail: Class.new } }

      it 'properly validates the relation' do
        expect(subject.send("#{relation}_valid?", :test)).to eql(true)
        expect { subject.send("#{relation}_valid?", :fail) }.to raise_error(
          Grape::Roar::Extensions::Relations::\
          Exceptions::InvalidRelationError
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-roar-0.4.1 spec/extensions/relations/validations/active_record_spec.rb