Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldRequireParametersContract

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

Overview

Contract asserting the action requires resource parameters.

Instance Method Summary collapse

Instance Method Details

#apply(example_group) { ... } ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    The example group to which the contract is applied.

Yields:

  • Additional configuration or examples.



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 518

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

    context 'when the parameters do not include params for the resource' \
    do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_params) do
        option_with_default(options[:params], default: {})
          .dup
          .tap do |hsh|
            hsh.delete(configured_resource.singular_name)
          end
      end
      let(:configured_expected_error) do
        errors = Stannum::Errors.new.tap do |err|
          err[configured_resource.singular_name]
            .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

    context 'when the resource parameters are not a Hash' do
      let(:request) do
        Cuprum::Rails::Request.new(params: configured_params)
      end
      let(:configured_params) do
        option_with_default(options[:params], default: {})
          .merge(configured_resource.singular_name => 'invalid')
      end
      let(:configured_expected_error) do
        errors = Stannum::Errors.new.tap do |err|
          err[configured_resource.singular_name].add(
            Stannum::Constraints::Type::TYPE,
            allow_empty: true,
            required:    true,
            type:        Hash
          )
        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