Module: Cuprum::Rails::RSpec::Contracts::Actions::DestroyContracts::ShouldBeADestroyActionContract

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

Overview

Contract asserting the action implements the destroy action interface.

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):

  • examples_on_failure (#to_proc)

    Extra examples to run for the failing cases.

  • examples_on_success (#to_proc)

    Extra examples to run for the passing case.

  • expected_value_on_success (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.

  • primary_key_value (Object)

    The value of the primary key for the missing entity.

Yields:

  • Additional examples to run for the passing case.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cuprum/rails/rspec/contracts/actions/destroy_contracts.rb', line 38

contract do |existing_entity:, **options, &block|
  include Cuprum::Rails::RSpec::Contracts::ActionContracts
  include Cuprum::Rails::RSpec::Contracts::Actions::DestroyContracts

  # :nocov:
  if options[:examples_on_success] && block
    raise ArgumentError, 'provide either :examples_on_success or a block'
  elsif block
    options[:examples_on_success] = block
  end
  # :nocov:

  should_not_destroy_the_entity = lambda do
    it 'should not destroy the entity' do
      expect { call_action }
        .not_to change(configured_resource.entity_class, :count)
    end

    # :nocov:
    if options[:examples_on_failure]
      instance_exec(&options[:examples_on_failure])
    end
    # :nocov:
  end

  include_contract 'should be a resource action'

  include_contract(
    'should require primary key',
    params: options[:params],
    &should_not_destroy_the_entity
  )

  include_contract(
    'should require existing entity',
    params:            options[:params],
    primary_key_value: options[:primary_key_value],
    &should_not_destroy_the_entity
  )

  include_contract(
    'should destroy the entity',
    existing_entity: existing_entity,
    expected_value:  options[:expected_value_on_success],
    params:          options[:params],
    &options[:examples_on_success]
  )
end