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

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

Overview

Contract asserting the action implements the show action interface.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, existing_entity: , invalid_attributes: , 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 update.

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

    A set of attributes that will fail validation.

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

    A set of attributes that will pass validation.

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 updated entity.

  • expected_attributes (Hash<String>)

    The expected attributes for both a failed validation and a returned entity.

  • expected_attributes_on_failure (Hash<String>)

    The expected attributes for a failed validation. Defaults to the value of invalid_attributes.

  • expected_attributes_on_success (Hash<String>)

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

  • expected_value_on_success (Hash<String>)

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

  • params (Hash<String>)

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

  • primary_key_value (Object)

    The value of the primary key for the missing entity.

Yields:

  • Additional examples to run for the passing case.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
# File 'lib/cuprum/rails/rspec/contracts/actions/update_contracts.rb', line 76

contract do |existing_entity:, invalid_attributes:, valid_attributes:, **options, &block| # rubocop:disable Layout/LineLength
  include Cuprum::Rails::RSpec::Contracts::ActionContracts
  include Cuprum::Rails::RSpec::Contracts::Actions::UpdateContracts

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

  configured_params = lambda do
    Cuprum::Rails::RSpec::Contracts::Actions::UpdateContracts.parameters(
      context:          self,
      existing_entity:  existing_entity,
      resource:         configured_resource,
      valid_attributes: valid_attributes,
      **options
    )
  end

  should_not_update_the_entity = lambda do
    it 'should not update the entity' do
      entity = existing_entity
      entity = instance_exec(&entity) if entity.is_a?(Proc)

      expect { call_action }
        .not_to(change { entity.reload.attributes })
    end

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

  include_contract 'should be a resource action',
    require_permitted_attributes: true

  include_contract(
    'should require primary key',
    params: configured_params,
    &should_not_update_the_entity
  )

  include_contract(
    'should require parameters',
    params: configured_params,
    &should_not_update_the_entity
  )

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

  include_contract(
    'should validate attributes',
    existing_entity:     existing_entity,
    expected_attributes: options.fetch(
      :expected_attributes,
      options[:expected_attributes_on_failure]
    ),
    invalid_attributes:  invalid_attributes,
    params:              configured_params,
    &should_not_update_the_entity
  )

  include_contract(
    'should update the entity',
    existing_entity:     existing_entity,
    expected_attributes: options.fetch(
      :expected_attributes,
      options[:expected_attributes_on_success]
    ),
    expected_value:      options[:expected_value_on_success],
    valid_attributes:    valid_attributes,
    params:              configured_params,
    &options[:examples_on_success]
  )
end