-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
view_example_group_spec.rb
365 lines (313 loc) · 11.5 KB
/
view_example_group_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require 'support/group_failure_formatter'
module RSpec::Rails
RSpec.describe ViewExampleGroup do
it_behaves_like "an rspec-rails example group mixin", :view,
'./spec/views/', '.\\spec\\views\\'
describe 'automatic inclusion of helpers' do
module ::ThingsHelper; end
module ::Namespaced; module ThingsHelper; end; end
it 'includes the helper with the same name' do
group = RSpec::Core::ExampleGroup.describe('things/show.html.erb') do
def self.helper(*); end # Stub method
end
allow(group).to receive(:helper)
expect(group).to receive(:helper).with(ThingsHelper)
group.class_exec do
include ViewExampleGroup
end
end
it 'includes the namespaced helper with the same name' do
group = RSpec::Core::ExampleGroup.describe('namespaced/things/show.html.erb') do
def self.helper(*); end # Stub method
end
allow(group).to receive(:helper)
expect(group).to receive(:helper).with(Namespaced::ThingsHelper)
group.class_exec do
include ViewExampleGroup
end
end
it 'operates normally when no helper with the same name exists' do
raise 'unexpected constant found' if Object.const_defined?('ClocksHelper')
expect {
RSpec::Core::ExampleGroup.describe 'clocks/show.html.erb' do
include ViewExampleGroup
end
}.not_to raise_error
end
it 'operates normally when the view has no path and there is a Helper class defined' do
class ::Helper; end
expect {
RSpec::Core::ExampleGroup.describe 'show.html.erb' do
include ViewExampleGroup
end
}.not_to raise_error
end
context 'application helper exists' do
before do
unless Object.const_defined? 'ApplicationHelper'
module ::ApplicationHelper; end
@application_helper_defined = true
end
end
after do
if @application_helper_defined
Object.__send__ :remove_const, 'ApplicationHelper'
end
end
it 'includes the application helper' do
group = RSpec::Core::ExampleGroup.describe('bars/new.html.erb') do
def self.helper(*); end # Stub method
end
allow(group).to receive(:helper)
expect(group).to receive(:helper).with(ApplicationHelper)
group.class_exec do
include ViewExampleGroup
end
end
end
context 'no application helper exists' do
before do
if Object.const_defined? 'ApplicationHelper'
@application_helper = ApplicationHelper
Object.__send__ :remove_const, 'ApplicationHelper'
end
end
after do
if defined?(@application_helper)
ApplicationHelper = @application_helper
end
end
it 'operates normally' do
expect {
RSpec::Core::ExampleGroup.describe 'foos/edit.html.erb' do
include ViewExampleGroup
end
}.not_to raise_error
end
end
end
describe "routes helpers collides with asset helpers" do
let(:view_spec) do
Class.new do
include ActionView::Helpers::AssetTagHelper
include ViewExampleGroup::ExampleMethods
end.new
end
let(:test_routes) do
ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw { resources :images }
end
end
it "uses routes helpers" do
allow(::Rails.application).to receive(:routes).and_return(test_routes)
expect(view_spec.image_path(double(to_param: "42")))
.to eq "/images/42"
end
end
describe "#render" do
let(:view_spec) do
Class.new do
local = Module.new do
def received
@received ||= []
end
def render(options = {}, local_assigns = {}, &block)
received << [options, local_assigns, block]
end
def _assigns
{}
end
end
include local
def _default_file_to_render; end # Stub method
include ViewExampleGroup::ExampleMethods
end.new
end
context "given no input" do
it "sends render(:template => (described file)) to the view" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new" }
view_spec.render
expect(view_spec.received.first).to eq([{ template: "widgets/new" }, {}, nil])
end
it "converts the filename components into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{ template: "widgets/new", locales: [:en], formats: [:html], handlers: [:erb] }, {}, nil])
end
it "converts the filename with variant into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html+fancy.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{ template: "widgets/new", locales: [:en], formats: [:html], handlers: [:erb], variants: [:fancy] }, {}, nil])
end
it "converts the filename without format into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{ template: "widgets/new", locales: [:en], handlers: [:erb] }, {}, nil])
end
end
context "given a string" do
it "sends string as the first arg to render" do
view_spec.render('arbitrary/path')
expect(view_spec.received.first).to eq(["arbitrary/path", {}, nil])
end
end
context "given a hash" do
it "sends the hash as the first arg to render" do
view_spec.render(foo: 'bar')
expect(view_spec.received.first).to eq([{ foo: "bar" }, {}, nil])
end
end
end
describe '#params' do
let(:view_spec) do
Class.new do
include ViewExampleGroup::ExampleMethods
def controller
@controller ||= Struct.new(:params).new(nil)
end
end.new
end
it 'delegates to the controller' do
expect(view_spec.controller).to receive(:params).and_return({})
view_spec.params[:foo] = 1
end
end
describe "#_controller_path" do
let(:view_spec) do
Class.new do
include ViewExampleGroup::ExampleMethods
def _default_file_to_render; end
end.new
end
context "with a common _default_file_to_render" do
it "it returns the directory" do
allow(view_spec).to receive(:_default_file_to_render)
.and_return("things/new.html.erb")
expect(view_spec.__send__(:_controller_path))
.to eq("things")
end
end
context "with a nested _default_file_to_render" do
it "it returns the directory path" do
allow(view_spec).to receive(:_default_file_to_render)
.and_return("admin/things/new.html.erb")
expect(view_spec.__send__(:_controller_path))
.to eq("admin/things")
end
end
end
describe "#view" do
let(:view_spec) do
Class.new do
def _view; end # Stub method
include ViewExampleGroup::ExampleMethods
end.new
end
it "delegates to _view" do
view = double("view")
allow(view_spec).to receive(:_view) { view }
expect(view_spec.view).to eq(view)
end
# Regression test from rspec/rspec-rails#833
it 'is accessible to configuration-level hooks' do
run_count = 0
RSpec.configuration.before(:each, type: :view) do
# `view` is provided from the view example type, and serves to
# demonstrate this hook is run in the correct context.
allow(view).to receive(:render) { :value }
run_count += 1
end
group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do
specify { expect(view.render).to eq(:value) }
end
group.run(failure_reporter)
expect(failure_reporter.exceptions).to eq []
expect(run_count).to eq 1
end
# Rails expects the first prefix to be the controller path.
# @see ActionView::AbstractRenderer::ObjectRendering#initialize
# Regression test for rspec/rspec-rails#2729
it 'injects controller path as first prefix' do
prefixes = []
RSpec.describe "a view spec" do
include ::RSpec::Rails::ViewExampleGroup
def _controller_path
"example/path"
end
specify do
prefixes = view.lookup_context.prefixes
end
end.run
expect(prefixes).to start_with("example/path")
end
end
describe "#template" do
let(:view_spec) do
Class.new do
include ViewExampleGroup::ExampleMethods
def _view; end
end.new
end
before { allow(RSpec).to receive(:deprecate) }
it "is deprecated" do
expect(RSpec).to receive(:deprecate)
view_spec.template
end
it "delegates to #view" do
expect(view_spec).to receive(:view)
view_spec.template
end
end
describe '#stub_template' do
let(:view_spec_group) do
RSpec.describe "a view spec" do
include ::RSpec::Rails::ViewExampleGroup
end
end
it 'prepends an ActionView::FixtureResolver to the view path' do
result = :not_loaded
view_spec_group.specify do
stub_template('some_path/some_template' => 'stubbed-contents')
result = view.view_paths.first
end
view_spec_group.run
expect(result).to be_instance_of(ActionView::FixtureResolver)
data = result.respond_to?(:data) ? result.data : result.hash
expect(data).to eq('some_path/some_template' => 'stubbed-contents')
end
it 'caches FixtureResolver instances between examples' do
example_one_view_paths = :not_set
example_two_view_paths = :not_set
view_spec_group.specify do
stub_template('some_path/some_template' => 'stubbed-contents')
example_one_view_paths = view.view_paths
end
view_spec_group.specify do
stub_template('some_path/some_template' => 'stubbed-contents')
example_two_view_paths = view.view_paths
end
view_spec_group.run
expect(example_one_view_paths.first).to eq(example_two_view_paths.first)
end
it 'caches FixtureResolver instances between example groups' do
example_one_view_paths = :not_set
example_two_view_paths = :not_set
RSpec.describe "a view spec" do
include ::RSpec::Rails::ViewExampleGroup
specify do
stub_template('some_path/some_template' => 'stubbed-contents')
example_one_view_paths = view.view_paths
end
end.run
RSpec.describe "another view spec" do
include ::RSpec::Rails::ViewExampleGroup
specify do
stub_template('some_path/some_template' => 'stubbed-contents')
example_two_view_paths = view.view_paths
end
end.run
expect(example_one_view_paths.first).to eq(example_two_view_paths.first)
end
end
end
end