Module: Cuprum::Rails::RSpec::Contracts::Actions::UpdateContracts::ShouldUpdateTheEntityContract

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

Overview

Contract asserting the action updates the entity.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, existing_entity: , valid_attributes: , **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 find.

  • valid_attributes (Hash<String>) (defaults to: )

    A set of attributes that will pass validation.

Options Hash (**options):

  • expected_attributes (Hash<String>)

    The expected attributes for the returned object. Defaults to the value of valid_attributes.

  • expected_value (Hash<String>)

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

  • params (Hash<String>)

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

Yields:

  • Additional examples.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cuprum/rails/rspec/contracts/actions/update_contracts.rb', line 185

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

    context 'with valid parameters' do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_params) do
        Cuprum::Rails::RSpec::Contracts::Actions::UpdateContracts
          .parameters(
            context:          self,
            existing_entity:  existing_entity,
            resource:         configured_resource,
            valid_attributes: valid_attributes,
            **options
          )
      end
      let(:configured_existing_entity) do
        option_with_default(existing_entity)
      end
      let(:configured_valid_attributes) do
        option_with_default(valid_attributes)
      end
      let(:configured_expected_attributes) do
        existing_attributes =
          configured_existing_entity
            .attributes
            .tap { |hsh| hsh.delete('updated_at') }

        option_with_default(
          options[:expected_attributes],
          default: existing_attributes.merge(configured_valid_attributes)
        )
      end
      let(:configured_expected_entity) do
        configured_resource
          .entity_class
          .find(
            configured_existing_entity[configured_resource.primary_key]
          )
      end
      let(:configured_expected_value) do
        resource_name = configured_resource.singular_name

        option_with_default(
          options[:expected_value],
          default: {
            resource_name => configured_expected_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 update the entity' do
        expect { call_action }
          .to(
            change do
              configured_existing_entity
                .reload
                .attributes
                .tap { |hsh| hsh.delete('updated_at') }
            end
            .to(be >= configured_expected_attributes)
          )
      end

      instance_exec(&block) if block
    end
  end
end