Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldRequireExistingEntityContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/rails/rspec/contracts/action_contracts.rb

Overview

Contract asserting the action requires a valid entity.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, **options) { ... } ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    The example group to which the contract is applied.

Options Hash (**options):

  • params (Hash<String>)

    The parameters used to build the request. Defaults to the id of the entity.

  • primary_key_value (Object)

    The value of the primary key for the missing entity.

Yields:

  • Additional configuration or examples.



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 452

contract do |**options, &block|
  describe '#call' do
    include Cuprum::Rails::RSpec::ContractHelpers

    context 'when the entity does not exist' do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_primary_key_value) do
        option_with_default(
          options[:primary_key_value],
          default: 0
        )
      end
      let(:configured_params) do
        option_with_default(
          options[:params],
          default: {}
        )
          .merge({ 'id' => configured_primary_key_value })
      end
      let(:expected_error) do
        Cuprum::Collections::Errors::NotFound.new(
          attribute_name:  configured_resource.primary_key.to_s,
          attribute_value: configured_primary_key_value,
          collection_name: configured_resource.name,
          primary_key:     true
        )
      end

      before(:example) do
        primary_key_name = configured_resource.primary_key

        resource
          .entity_class
          .where(primary_key_name => configured_primary_key_value)
          .destroy_all
      end

      it 'should return a failing result' do
        expect(call_action)
          .to be_a_failing_result
          .with_error(expected_error)
      end

      instance_exec(&block) if block
    end
  end
end