Module: Cuprum::Rails::RSpec::Contracts::Actions::NewContracts::ShouldBuildTheEntityContract

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

Overview

Contract asserting the action builds a new entity.

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

  • expected_attributes (Hash<String>)

    The expected attributes for the returned object.

  • expected_value (Hash<String>)

    The expected value for the passing result. Defaults to a Hash with the built entity.

  • params (Hash<String>)

    The parameters used to build the request. Defaults to the id of the entity and the given attributes.

Yields:

  • Additional examples.



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
# File 'lib/cuprum/rails/rspec/contracts/actions/new_contracts.rb', line 73

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

    let(:request) do
      Cuprum::Rails::Request.new(params: configured_params)
    end
    let(:configured_params) do
      option_with_default(options[:params], default: {})
    end
    let(:configured_expected_attributes) do
      option_with_default(options[:expected_attributes], default: {})
    end
    let(:configured_expected_entity) do
      configured_resource
        .entity_class
        .new(configured_expected_attributes)
    end
    let(:configured_expected_value) do
      resource_name = configured_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

    instance_exec(&block) if block
  end
end