Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeABuildOneCommandContract

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

Overview

Contract validating the behavior of a Build 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.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 193

contract do |allow_extra_attributes:|
  include Stannum::RSpec::Matchers

  describe '#call' do
    shared_examples 'should build the entity' do
      it { expect(result).to be_a_passing_result }

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

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

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

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

      include_examples 'should build the entity'
    end

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

      include_examples 'should build the entity'
    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 build the entity'
    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 build the entity'
      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_type,
            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