Module: Cuprum::Rails::RSpec::Contracts::RoutesContracts::ShouldDefineCollectionRouteContract
- Extended by:
- RSpec::SleepingKingStudios::Contract
- Defined in:
- lib/cuprum/rails/rspec/contracts/routes_contracts.rb
Overview
Contract asserting that the given collection route helper is defined.
Instance Method Summary collapse
-
#apply(example_group, constructor_keywords: []) ⇒ Object
Adds the contract to the example group.
Instance Method Details
#apply(example_group, constructor_keywords: []) ⇒ Object
Adds the contract to the example group.
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 |
# File 'lib/cuprum/rails/rspec/contracts/routes_contracts.rb', line 25 contract do |action_name:, path:, **| [:wildcards] = .fetch(:wildcards, {}).stringify_keys expected_wildcards = path.split('/').select { |str| str.start_with?(':') } describe "##{action_name}_path" do let(:method_name) { "#{action_name}_path" } let(:wildcards) { [:wildcards] } let(:expected) do expected_wildcards.reduce(path) do |resolved, key| resolved.sub(key, resolve_wildcard(key)) end end let(:error_class) do Cuprum::Rails::Routes::MissingWildcardError end let(:error_message) do "missing wildcard #{expected_wildcards.first}" end def resolve_wildcard(key) key = key.sub(/\A:/, '') value = wildcards.fetch(key) { wildcards.fetch(key.sub(/_id\z/, '')) } return value.to_s unless value.class.respond_to?(:primary_key) primary_key = value.class.primary_key value[primary_key].to_s end it 'should define the helper method' do expect(subject) .to respond_to(method_name) .with(0).arguments .and_any_keywords end if expected_wildcards.empty? it { expect(subject.send(method_name)).to be == expected } else it 'should raise an exception' do expect { subject.send(method_name) } .to raise_error(error_class, ) end expected_wildcards.each do |key| describe "with wildcards: missing #{key}" do let(:wildcards) do wildcard = key[1..] super().except(wildcard, wildcard[...-3]) end let(:error_message) { "missing wildcard #{key}" } it 'should raise an exception' do expect { subject.send(method_name, **wildcards) } .to raise_error(error_class, ) end end context "when the routes defines wildcards: missing #{key}" do let(:wildcards) do wildcard = key[1..] super().except(wildcard, wildcard[...-3]) end let(:error_message) { "missing wildcard #{key}" } it 'should raise an exception' do expect { subject.with_wildcards(wildcards).send(method_name) } .to raise_error(error_class, ) end end end describe 'with wildcards: matching wildcards' do it 'should generate the path' do expect(subject.send(method_name, **wildcards)).to be == expected end end context 'when the routes defines wildcards: matching wildcards' do it 'should generate the path' do expect(subject.with_wildcards(wildcards).send(method_name)) .to be == expected end describe 'with wildcards: value' do let(:other_wildcards) do expected_wildcards.each.with_index.to_h end it 'should generate the path' do expect( subject .with_wildcards(other_wildcards) .send(method_name, **wildcards) ) .to be == expected end end end end describe 'with wildcards: extra wildcards' do let(:wildcards) { super().merge('other_id' => 'value') } it 'should generate the path' do expect(subject.send(method_name, **wildcards)).to be == expected end end context 'when the routes defines wildcards: extra wildcards' do let(:wildcards) { super().merge('other_id' => 'value') } it 'should generate the path' do expect(subject.with_wildcards(wildcards).send(method_name)) .to be == expected end end end end |