Module: Cuprum::Rails::RSpec::Contracts::ResponderContracts::ShouldImplementTheResponderMethodsContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/rails/rspec/contracts/responder_contracts.rb

Overview

Contract validating the interface for a responder.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, constructor_keywords: []) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • constructor_keywords (Array<Symbol>) (defaults to: [])

    additional keywords that are required by the constructor.

  • controller_name (String)

    the name of the test controller.



22
23
24
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
# File 'lib/cuprum/rails/rspec/contracts/responder_contracts.rb', line 22

contract do |
  constructor_keywords: [],
  controller_name:      'Spec::CustomController'
|
  def self.let?(method_name, &block)
    let(method_name, &block) unless method_defined?(method_name)
  end

  let?(:resource_options) { { name: 'books' } }
  let?(:resource) do
    Cuprum::Rails::Resource.new(**resource_options)
  end

  example_class(controller_name) do |klass|
    configured_resource = resource

    klass.define_singleton_method(:resource) do
      @resource ||= configured_resource
    end
  end

  describe '.new' do
    let(:expected_keywords) do
      %i[
        action_name
        controller
        member_action
        request
        resource
      ]
    end

    it 'should define the constructor' do
      expect(described_class)
        .to respond_to(:new)
        .with(0).arguments
        .and_keywords(*expected_keywords, *constructor_keywords)
        .and_any_keywords
    end
  end

  describe '#action_name' do
    include_examples 'should define reader',
      :action_name,
      -> { action_name }
  end

  describe '#call' do
    let(:result) { Cuprum::Result.new(value: :ok) }

    def ignore_exceptions
      yield
    rescue StandardError
      # Do nothing
    end

    it { expect(responder).to respond_to(:call).with(1).argument }

    it 'should set the result' do
      expect { ignore_exceptions { responder.call(result) } }
        .to change(responder, :result)
        .to be == result
    end
  end

  describe '#controller' do
    include_examples 'should define reader',
      :controller,
      -> { controller }
  end

  describe '#controller_name' do
    include_examples 'should define reader',
      :controller_name,
      controller_name
  end

  describe '#member_action?' do
    include_examples 'should define predicate',
      :member_action?,
      -> { !!constructor_options[:member_action] } # rubocop:disable Style/DoubleNegation

    context 'when initialized with member_action: false' do
      let(:constructor_options) { super().merge(member_action: false) }

      it { expect(responder.member_action?).to be false }
    end

    context 'when initialized with member_action: true' do
      let(:constructor_options) { super().merge(member_action: true) }

      it { expect(responder.member_action?).to be true }
    end
  end

  describe '#request' do
    include_examples 'should define reader', :request, -> { request }
  end

  describe '#resource' do
    include_examples 'should define reader',
      :resource,
      -> { controller.class.resource }
  end

  describe '#result' do
    include_examples 'should define reader', :result, nil
  end

  describe '#routes' do
    include_examples 'should define reader', :routes

    let(:resource_options) { super().merge(name: 'books') }

    it 'should return the resource routes' do
      expect(responder.routes)
        .to be_a Cuprum::Rails::Routing::PluralRoutes
    end

    it { expect(responder.routes.base_path).to be == '/books' }

    it { expect(responder.routes.index_path).to be == '/books' }

    it { expect(responder.routes.parent_path).to be == '/' }

    it { expect(responder.routes.show_path(0)).to be == '/books/0' }

    it { expect(responder.routes.wildcards).to be == {} }

    context 'when the request has path params' do
      let(:path_params) { { 'author_id' => 0 } }
      let(:request) do
        Cuprum::Rails::Request.new(path_params: path_params)
      end

      it { expect(responder.routes.wildcards).to be == path_params }
    end

    context 'when initialized with a singular resource' do
      let(:resource_options) do
        super().merge(name: 'book', singular: true)
      end

      it 'should return the resource routes' do
        expect(responder.routes)
          .to be_a Cuprum::Rails::Routing::SingularRoutes
      end

      it { expect(responder.routes.base_path).to be == '/book' }

      it { expect(responder.routes.show_path).to be == '/book' }
    end

    context 'when initialized with a resource with custom routes' do
      let(:resource_options) do
        super().merge(
          routes: Cuprum::Rails::Routes.new(base_path: '/path/to/books')
        )
      end

      it { expect(responder.routes.base_path).to be == '/path/to/books' }
    end

    context 'when initialized with a resource with ancestors' do
      let(:base_path) { '/authors/:author_id/series/:series_id' }
      let(:authors_resource) do
        Cuprum::Rails::Resource.new(name: 'authors')
      end
      let(:series_resource) do
        Cuprum::Rails::Resource.new(
          name:          'series',
          singular_name: 'series',
          parent:        authors_resource
        )
      end
      let(:resource_options) { super().merge(parent: series_resource) }

      it 'should set the scoped base path' do
        expect(responder.routes.base_path).to be == "#{base_path}/books"
      end

      context 'when the request has path params' do
        let(:path_params) { { 'author_id' => 0, 'series_id' => 1 } }
        let(:parent_path) { '/authors/0/series/1' }
        let(:request) do
          Cuprum::Rails::Request.new(path_params: path_params)
        end

        it { expect(responder.routes.wildcards).to be == path_params }

        it 'should set the scoped index path' do
          expect(responder.routes.index_path)
            .to be == "#{parent_path}/books"
        end

        it 'should set the scoped parent path' do
          expect(responder.routes.parent_path)
            .to be == parent_path
        end

        it 'should set the scoped show path' do
          expect(responder.routes.show_path(2))
            .to be == "#{parent_path}/books/2"
        end
      end
    end
  end
end