Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAnAssignOneCommandContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/command_contracts.rb

Overview

Contract validating the behavior of an AssignOne command implementation.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, allow_extra_attributes: ) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • allow_extra_attributes (Boolean) (defaults to: )

    if false, the command should fail if given attributes not defined for the entity.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 19

contract do |allow_extra_attributes:|
  describe '#call' do
    shared_examples 'should assign the attributes' do
      it { expect(result).to be_a_passing_result }

      it { expect(result.value).to be_a entity.class }

      it { expect(result.value).to be == expected_value }
    end

    let(:attributes) { {} }
    let(:result) do
      command.call(attributes: attributes, entity: entity)
    end
    let(:expected_attributes) do
      initial_attributes.merge(attributes)
    end
    let(:expected_value) do
      defined?(super()) ? super() : expected_attributes
    end

    it 'should validate the :attributes keyword' do
      expect(command)
        .to validate_parameter(:call, :attributes)
        .using_constraint(
          Stannum::Constraints::Types::HashWithIndifferentKeys.new
        )
    end

    it 'should validate the :entity keyword' do
      expect(command)
        .to validate_parameter(:call, :entity)
        .using_constraint(entity_type)
        .with_parameters(attributes: {}, entity: nil)
    end

    describe 'with an empty attributes hash' do
      let(:attributes) { {} }

      include_examples 'should assign the attributes'
    end

    describe 'with an attributes hash with partial attributes' do
      let(:attributes) { { title: 'Gideon the Ninth' } }

      include_examples 'should assign the attributes'
    end

    describe 'with an attributes hash with full attributes' do
      let(:attributes) do
        {
          title:    'Gideon the Ninth',
          author:   'Tamsyn Muir',
          series:   'The Locked Tomb',
          category: 'Horror'
        }
      end

      include_examples 'should assign the attributes'
    end

    describe 'with an attributes hash with extra attributes' do
      let(:attributes) do
        {
          title:     'The Book of Lost Tales',
          audiobook: true
        }
      end

      if allow_extra_attributes
        include_examples 'should assign the attributes'
      else
        # :nocov:
        let(:valid_attributes) do
          defined?(super()) ? super() : expected_attributes.keys
        end
        let(:expected_error) do
          Cuprum::Collections::Errors::ExtraAttributes.new(
            entity_class:     entity.class,
            extra_attributes: %w[audiobook],
            valid_attributes: valid_attributes
          )
        end

        it 'should return a failing result' do
          expect(result).to be_a_failing_result.with_error(expected_error)
        end
        # :nocov:
      end
    end

    context 'when the entity has existing attributes' do
      let(:initial_attributes) do
        # :nocov:
        if defined?(super())
          super().merge(fixtures_data.first)
        else
          fixtures_data.first
        end
        # :nocov:
      end

      describe 'with an empty attributes hash' do
        let(:attributes) { {} }

        include_examples 'should assign the attributes'
      end

      describe 'with an attributes hash with partial attributes' do
        let(:attributes) { { title: 'Gideon the Ninth' } }

        include_examples 'should assign the attributes'
      end

      describe 'with an attributes hash with full attributes' do
        let(:attributes) do
          {
            title:    'Gideon the Ninth',
            author:   'Tamsyn Muir',
            series:   'The Locked Tomb',
            category: 'Horror'
          }
        end

        include_examples 'should assign the attributes'
      end

      describe 'with an attributes hash with extra attributes' do
        let(:attributes) do
          {
            title:     'The Book of Lost Tales',
            audiobook: true
          }
        end

        if allow_extra_attributes
          include_examples 'should assign the attributes'
        else
          # :nocov:
          let(:valid_attributes) do
            defined?(super()) ? super() : expected_attributes.keys
          end
          let(:expected_error) do
            Cuprum::Collections::Errors::ExtraAttributes.new(
              entity_class:     entity.class,
              extra_attributes: %w[audiobook],
              valid_attributes: valid_attributes
            )
          end

          it 'should return a failing result' do
            expect(result)
              .to be_a_failing_result
              .with_error(expected_error)
          end
          # :nocov:
        end
      end
    end
  end
end