Skip to content

Commit 489cdc9

Browse files
committed
Merge pull request #949 from dabrorius/add_routing_specs
Add routing specs
2 parents cf4aac1 + 1dbf134 commit 489cdc9

File tree

2 files changed

+110
-17
lines changed

2 files changed

+110
-17
lines changed

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
134134
end
135135
end
136136

137-
def namespace(space = nil, options = {}, &block)
137+
def namespace(space = nil, options = {}, &block)
138138
if space || block_given?
139139
within_namespace do
140140
previous_namespace_description = @namespace_description

spec/grape/dsl/routing_spec.rb

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,43 @@ class Dummy
6464
expect(app2.inheritable_setting.to_hash[:namespace_stackable]).to eq(:mount_path => ['/app1', '/app2'])
6565
end
6666
end
67-
xdescribe '.route' do
68-
it 'does some thing'
67+
68+
describe '.route' do
69+
before do
70+
allow(subject).to receive(:endpoints).and_return([])
71+
allow(subject).to receive(:route_end)
72+
allow(subject).to receive(:reset_validations!)
73+
end
74+
75+
it 'marks end of the route' do
76+
expect(subject).to receive(:route_end)
77+
subject.route(:any)
78+
end
79+
80+
it 'resets validations' do
81+
expect(subject).to receive(:reset_validations!)
82+
subject.route(:any)
83+
end
84+
85+
it 'defines a new endpoint' do
86+
expect { subject.route(:any) }
87+
.to change{ subject.endpoints.count }.from(0).to(1)
88+
end
89+
90+
it 'generates correct endpoint options' do
91+
allow(subject).to receive(:route_setting).with(:description).and_return(fiz: 'baz')
92+
allow(Grape::DSL::Configuration).to receive(:stacked_hash_to_hash).and_return(nuz: 'naz')
93+
94+
expect(Grape::Endpoint).to receive(:new) do |inheritable_setting, endpoint_options|
95+
puts endpoint_options
96+
expect(endpoint_options[:method]).to eq :get
97+
expect(endpoint_options[:path]).to eq '/foo'
98+
expect(endpoint_options[:for]).to eq subject
99+
expect(endpoint_options[:route_options]).to eq(foo: 'bar', fiz: 'baz', params: { nuz: 'naz' })
100+
end.and_yield
101+
102+
subject.route(:get, '/foo', { foo: 'bar' }, &proc{})
103+
end
69104
end
70105

71106
describe '.get' do
@@ -117,32 +152,90 @@ class Dummy
117152
end
118153
end
119154

120-
xdescribe '.namespace' do
121-
it 'does some thing'
155+
describe '.namespace' do
156+
let(:new_namespace) { Object.new }
157+
158+
it 'creates a new namespace with given name and options' do
159+
expect(subject).to receive(:within_namespace).and_yield
160+
expect(subject).to receive(:nest).and_yield
161+
expect(Namespace).to receive(:new).with(:foo, foo: 'bar').and_return(new_namespace)
162+
expect(subject).to receive(:namespace_stackable).with(:namespace, new_namespace)
163+
164+
subject.namespace :foo, foo: 'bar', &proc{}
165+
end
166+
167+
it 'calls #joined_space_path on Namespace' do
168+
result_of_namspace_stackable = Object.new
169+
allow(subject).to receive(:namespace_stackable).and_return(result_of_namspace_stackable)
170+
expect(Namespace).to receive(:joined_space_path).with(result_of_namspace_stackable)
171+
subject.namespace
172+
end
122173
end
123174

124-
xdescribe '.group' do
125-
it 'does some thing'
175+
describe '.group' do
176+
it 'is alias to #namespace' do
177+
expect(subject.method(:group)).to eq subject.method(:namespace)
178+
end
126179
end
127180

128-
xdescribe '.resource' do
129-
it 'does some thing'
181+
describe '.resource' do
182+
it 'is alias to #namespace' do
183+
expect(subject.method(:resource)).to eq subject.method(:namespace)
184+
end
130185
end
131186

132-
xdescribe '.resources' do
133-
it 'does some thing'
187+
describe '.resources' do
188+
it 'is alias to #namespace' do
189+
expect(subject.method(:resources)).to eq subject.method(:namespace)
190+
end
134191
end
135192

136-
xdescribe '.segment' do
137-
it 'does some thing'
193+
describe '.segment' do
194+
it 'is alias to #namespace' do
195+
expect(subject.method(:segment)).to eq subject.method(:namespace)
196+
end
138197
end
139198

140-
xdescribe '.routes' do
141-
it 'does some thing'
199+
describe '.routes' do
200+
let(:routes) { Object.new }
201+
202+
it 'returns value received from #prepare_routes' do
203+
expect(subject).to receive(:prepare_routes).and_return(routes)
204+
expect(subject.routes).to eq routes
205+
end
206+
207+
context 'when #routes was already called once' do
208+
before do
209+
allow(subject).to receive(:prepare_routes).and_return(routes)
210+
subject.routes
211+
end
212+
it 'it does not call prepare_routes again' do
213+
expect(subject).to_not receive(:prepare_routes)
214+
expect(subject.routes).to eq routes
215+
end
216+
end
142217
end
143218

144-
xdescribe '.route_param' do
145-
it 'does some thing'
219+
describe '.route_param' do
220+
it 'calls #namespace with given params' do
221+
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
222+
subject.route_param('foo', {}, &proc{})
223+
end
224+
225+
let(:regex) { /(.*)/ }
226+
let!(:options) { { requirements: regex } }
227+
it 'nests requirements option under param name' do
228+
expect(subject).to receive(:namespace) do |param, options|
229+
expect(options[:requirements][:foo]).to eq regex
230+
end
231+
subject.route_param('foo', options, &proc{})
232+
end
233+
234+
it 'does not modify options parameter' do
235+
allow(subject).to receive(:namespace)
236+
expect { subject.route_param('foo', options, &proc{}) }
237+
.to_not change { options }
238+
end
146239
end
147240

148241
describe '.versions' do

0 commit comments

Comments
 (0)