Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAnUpdateOneCommandContract

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

Overview

Contract validating the behavior of an UpdateOne command implementation.

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.



1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 1282

contract do
  describe '#call' do
    let(:mapped_data) do
      defined?(super()) ? super() : data
    end
    let(:matching_data) { attributes }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end
    let(:primary_key_name) do
      defined?(super()) ? super() : 'id'
    end
    let(:scoped) do
      key    = primary_key_name
      value  = entity[primary_key_name.to_s]

      query.where { { key => value } }
    end

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

    context 'when the item does not exist in the collection' do
      let(:expected_error) do
        Cuprum::Collections::Errors::NotFound.new(
          attribute_name:  primary_key_name,
          attribute_value: attributes.fetch(
            primary_key_name.to_s,
            attributes[primary_key_name.intern]
          ),
          collection_name: collection_name,
          primary_key:     true
        )
      end
      let(:matching_data) { mapped_data.first }

      it 'should return a failing result' do
        expect(command.call(entity: entity))
          .to be_a_failing_result
          .with_error(expected_error)
      end

      it 'should not append an item to the collection' do
        expect { command.call(entity: entity) }
          .not_to(change { query.reset.count })
      end
    end

    context 'when the item exists in the collection' do
      let(:data) { fixtures_data }
      let(:matching_data) do
        mapped_data.first.merge(super())
      end

      it 'should return a passing result' do
        expect(command.call(entity: entity))
          .to be_a_passing_result
          .with_value(be == expected_data)
      end

      it 'should not append an item to the collection' do
        expect { command.call(entity: entity) }
          .not_to(change { query.reset.count })
      end

      it 'should set the attributes' do
        command.call(entity: entity)

        expect(scoped.to_a.first).to be == expected_data
      end
    end
  end
end