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

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

Overview

Contract asserting the action implements the create action interface.

Instance Method Summary collapse

Instance Method Details

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

  • 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):

  • duplicate_attributes (Hash<String>)

    A set of attributes for a duplicate entity.

  • 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_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 created entity.

  • params (Hash<String>)

    The parameters used to build the request. Defaults to the given attributes.

Yields:

  • Additional examples to run for the passing case.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'lib/cuprum/rails/rspec/contracts/actions/create_contracts.rb', line 48

contract do |invalid_attributes:, valid_attributes:, **options, &block|
  include Cuprum::Rails::RSpec::Contracts::ActionContracts
  include Cuprum::Rails::RSpec::Contracts::Actions::CreateContracts

  options = options.merge(valid_attributes: valid_attributes)
  configured_params = lambda do
    attributes =
      Cuprum::Rails::RSpec::ContractHelpers.option_with_default(
        valid_attributes,
        context: self
      )

    Cuprum::Rails::RSpec::ContractHelpers.option_with_default(
      options[:params],
      context: self,
      default: {
        configured_resource.singular_name => attributes
      }
    )
  end

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

  should_not_create_an_entity = lambda do
    it 'should not create an entity' do
      expect { call_action }
        .not_to change(configured_resource.entity_class, :count)
    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 parameters',
    params: configured_params,
    &should_not_create_an_entity
  )

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

  if options[:duplicate_attributes]
    include_contract(
      'should not create a duplicate entity',
      params:           configured_params,
      valid_attributes: options[:duplicate_attributes],
      &should_not_create_an_entity
    )
  end

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