Module: Cuprum::Rails::RSpec::Contracts::Actions::CreateContracts::ShouldCreateTheEntityContract

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

Overview

Contract asserting the action creates a new entity.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, 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.

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 given attributes.

  • valid_attributes (Hash<String>)

    A set of attributes that will pass validation.

Yields:

  • Additional examples.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
# File 'lib/cuprum/rails/rspec/contracts/actions/create_contracts.rb', line 153

contract do |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_valid_attributes) do
        option_with_default(valid_attributes)
      end
      let(:configured_params) do
        resource_name = resource.singular_name

        option_with_default(
          options[:params],
          default: {}
        )
          .merge({ resource_name => configured_valid_attributes })
      end
      let(:configured_expected_attributes) do
        option_with_default(
          options[:expected_attributes],
          default: configured_valid_attributes
        )
      end
      let(:configured_expected_entity) do
        action
          .resource
          .entity_class
          .where(configured_expected_attributes)
          .first
      end
      let(:configured_expected_value) do
        resource_name = 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 create the entity', :aggregate_failures do
        expect { call_action }
          .to change(configured_resource.entity_class, :count)
          .by(1)

        expect(
          action
            .resource
            .entity_class
            .where(configured_expected_attributes)
            .exists?
        ).to be true
      end

      instance_exec(&block) if block.is_a?(Proc)
    end
  end
end