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

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

Overview

Contract asserting the action destroys the specified 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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cuprum/rails/rspec/contracts/actions/destroy_contracts.rb', line 106

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

      it 'should destroy the entity', :aggregate_failures do
        expect { call_action }
          .to change(configured_resource.entity_class, :count)
          .by(-1)

        primary_key_name  = configured_resource.primary_key
        primary_key_value = configured_existing_entity[primary_key_name]
        expect(
          action
          .resource
          .entity_class
          .exists?(primary_key_name => primary_key_value)
        ).to be false
      end

      instance_exec(&block) if block
    end
  end
end