Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldFindTheEntityContract

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

Overview

Contract asserting the action finds and returns the requested entity.

Instance Method Summary collapse

Instance Method Details

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

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    The example group to which the contract is applied.

  • existing_entity (Object) (defaults to: )

    The existing entity to destroy.

Options Hash (**options):

  • expected_value (Hash<String>)

    The expected value for the passing result. Defaults to a Hash with the destroyed entity.

  • params (Hash<String>)

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

Yields:

  • Additional configuration or examples.



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 394

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

    context 'when the entity exists' do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_existing_entity) do
        option_with_default(existing_entity)
      end
      let(:configured_params) do
        resource_id =
          configured_existing_entity[configured_resource.primary_key]

        option_with_default(
          options[:params],
          default: { 'id' => resource_id }
        )
      end
      let(:configured_expected_value) do
        resource_name = configured_resource.singular_name

        option_with_default(
          options[:expected_value],
          default: { resource_name => configured_existing_entity }
        )
      end

      it 'should return a passing result' do
        expect(call_action)
          .to be_a_passing_result
          .with_value(configured_expected_value)
      end

      instance_exec(&block) if block
    end
  end
end