Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAValidateOneCommandContract

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

Overview

Contract validating the behavior of a ValidateOne command implementation.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, default_contract: ) ⇒ Object

Adds the contract to the example group.

Parameters:

  • default_contract (Boolean) (defaults to: )

    if true, the command defines a default contract.

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.



1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 1371

contract do |default_contract:|
  describe '#call' do
    it 'should validate the :contract keyword' do
      expect(command)
        .to validate_parameter(:call, :contract)
        .with_value(Object.new.freeze)
        .using_constraint(Stannum::Constraints::Base, optional: true)
    end

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

    describe 'with contract: nil' do
      if default_contract
        context 'when the entity does not match the default contract' do
          let(:attributes) { invalid_default_attributes }
          let(:expected_error) do
            Cuprum::Collections::Errors::FailedValidation.new(
              entity_class: entity.class,
              errors:       expected_errors
            )
          end

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

        context 'when the entity matches the default contract' do
          let(:attributes) { valid_default_attributes }

          it 'should return a passing result' do
            expect(command.call(entity: entity))
              .to be_a_passing_result
              .with_value(entity)
          end
        end
      else
        let(:attributes) { valid_attributes }
        let(:expected_error) do
          Cuprum::Collections::Errors::MissingDefaultContract.new(
            entity_class: entity.class
          )
        end

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

    describe 'with contract: value' do
      context 'when the entity does not match the contract' do
        let(:attributes) { invalid_attributes }
        let(:errors)     { contract.errors_for(entity) }
        let(:expected_error) do
          Cuprum::Collections::Errors::FailedValidation.new(
            entity_class: entity.class,
            errors:       errors
          )
        end

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

      context 'when the entity matches the contract' do
        let(:attributes) { valid_attributes }

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