Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldValidateAttributesContract
- Extended by:
- RSpec::SleepingKingStudios::Contract
- Defined in:
- lib/cuprum/rails/rspec/contracts/action_contracts.rb
Overview
Contract asserting the action validates the created or updated entity.
Instance Method Summary collapse
-
#apply(example_group, invalid_attributes: , expected_attributes: nil, **options) { ... } ⇒ Object
Adds the contract to the example group.
Instance Method Details
#apply(example_group, invalid_attributes: , expected_attributes: nil, **options) { ... } ⇒ Object
Adds the contract to the example group.
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 654 contract do |invalid_attributes:, **, &block| describe '#call' do include Cuprum::Rails::RSpec::ContractHelpers context 'when the resource params fail validation' do let(:request) do Cuprum::Rails::Request.new(params: configured_params) end let(:configured_invalid_attributes) do option_with_default(invalid_attributes) end let(:configured_params) do resource_name = configured_resource.singular_name option_with_default( [:params], default: {} ).merge({ resource_name => configured_invalid_attributes }) end let(:configured_existing_entity) do option_with_default([:existing_entity]) end let(:configured_expected_attributes) do option_with_default( [:expected_attributes], default: (configured_existing_entity&.attributes || {}).merge( configured_invalid_attributes ) ) end let(:configured_expected_entity) do if configured_existing_entity repository .find_or_create( qualified_name: resource.qualified_name ) .assign_one .call( attributes: configured_invalid_attributes, entity: configured_existing_entity.clone ) .value .tap(&:valid?) else action .resource .entity_class .new(configured_expected_attributes) .tap(&:valid?) end end let(:configured_expected_value) do matcher = be_a(configured_expected_entity.class) .and(have_attributes(configured_expected_entity.attributes)) option_with_default( [:expected_value], default: { configured_resource.singular_name => matcher } ) end let(:configured_expected_error) do errors = Cuprum::Rails::MapErrors .instance .call(native_errors: configured_expected_entity.errors) Cuprum::Collections::Errors::FailedValidation.new( entity_class: configured_resource.entity_class, errors: scope_validation_errors(errors) ) end def scope_validation_errors(errors) mapped_errors = Stannum::Errors.new resource_name = configured_resource.singular_name errors.each do |err| mapped_errors .dig(resource_name, *err[:path].map(&:to_s)) .add(err[:type], message: err[:message], **err[:data]) end mapped_errors end it 'should return a failing result' do expect(call_action) .to be_a_failing_result .with_value(deep_match(configured_expected_value)) .and_error(configured_expected_error) end instance_exec(&block) if block end end end |