Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldRequirePrimaryKeyContract

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

Overview

Contract asserting the action requires a primary key.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, **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):

  • params (Hash<String>)

    The parameters used to build the request. Defaults to an empty Hash.

Yields:

  • Additional configuration or examples.



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 600

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

    context 'when the parameters do not include a primary key' do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_params) do
        option_with_default(options[:params], default: {})
          .dup
          .tap { |hsh| hsh.delete('id') }
      end
      let(:configured_expected_error) do
        errors = Stannum::Errors.new.tap do |err|
          err['id'].add(Stannum::Constraints::Presence::TYPE)
        end

        Cuprum::Rails::Errors::InvalidParameters.new(errors: errors)
      end

      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
    end
  end
end