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

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

Overview

Contract asserting the action does not create a duplicate entity.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, valid_attributes: , primary_key: "id") { ... } ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    The example group to which the contract is applied.

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

    A set of attributes that will pass validation.

Yields:

  • Additional examples.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cuprum/rails/rspec/contracts/actions/create_contracts.rb', line 240

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

    context 'with duplicate 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_duplicate_entity) do
        configured_resource.entity_class.new(valid_attributes)
      end
      let(:configured_params) do
        resource_name = configured_resource.singular_name

        option_with_default(
          options[:params],
          default: {}
        )
          .merge({ resource_name => configured_valid_attributes })
      end
      let(:configured_expected_error) do
        primary_key_name  = configured_resource.primary_key.to_s
        primary_key_value = configured_duplicate_entity[primary_key_name]

        Cuprum::Collections::Errors::AlreadyExists.new(
          attribute_name:  primary_key_name,
          attribute_value: primary_key_value,
          collection_name: configured_resource.name,
          primary_key:     true
        )
      end

      before(:example) { configured_duplicate_entity.save! }

      it 'should return a failing result' do
        expect(call_action)
          .to be_a_failing_result
          .with_error(configured_expected_error)
      end

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