Skip to content

Commit 7e29219

Browse files
committed
Test Controller#use_adapter? deprecation
1 parent eca2b78 commit 7e29219

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

test/action_controller/serialization_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module ActionController
44
module Serialization
55
class ImplicitSerializerTest < ActionController::TestCase
6+
include ActiveSupport::Testing::Stream
67
class ImplicitSerializationTestController < ActionController::Base
78
def render_using_implicit_serializer
89
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@@ -327,6 +328,28 @@ def test_cache_expiration_on_update
327328
assert_equal 'application/json', @response.content_type
328329
assert_equal expected.to_json, @response.body
329330
end
331+
332+
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
333+
controller = Class.new(ImplicitSerializationTestController) {
334+
def use_adapter?
335+
false
336+
end
337+
}.new
338+
assert_match /adapter: false/, (capture(:stderr) {
339+
controller.get_serializer(@profile)
340+
})
341+
end
342+
343+
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
344+
controller = Class.new(ImplicitSerializationTestController) {
345+
def use_adapter?
346+
true
347+
end
348+
}.new
349+
assert_equal "", (capture(:stderr) {
350+
controller.get_serializer(@profile)
351+
})
352+
end
330353
end
331354
end
332355
end

test/test_helper.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,55 @@
1212

1313
require 'active_model_serializers'
1414

15+
# Use cleaner stream testing interface from Rails 5 if available
16+
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
17+
begin
18+
require "active_support/testing/stream"
19+
rescue LoadError
20+
module ActiveSupport
21+
module Testing
22+
module Stream #:nodoc:
23+
private
24+
25+
def silence_stream(stream)
26+
old_stream = stream.dup
27+
stream.reopen(IO::NULL)
28+
stream.sync = true
29+
yield
30+
ensure
31+
stream.reopen(old_stream)
32+
old_stream.close
33+
end
34+
35+
def quietly
36+
silence_stream(STDOUT) do
37+
silence_stream(STDERR) do
38+
yield
39+
end
40+
end
41+
end
42+
43+
def capture(stream)
44+
stream = stream.to_s
45+
captured_stream = Tempfile.new(stream)
46+
stream_io = eval("$#{stream}")
47+
origin_stream = stream_io.dup
48+
stream_io.reopen(captured_stream)
49+
50+
yield
51+
52+
stream_io.rewind
53+
return captured_stream.read
54+
ensure
55+
captured_stream.close
56+
captured_stream.unlink
57+
stream_io.reopen(origin_stream)
58+
end
59+
end
60+
end
61+
end
62+
end
63+
1564
class Foo < Rails::Application
1665
if Rails::VERSION::MAJOR >= 4
1766
config.eager_load = false

0 commit comments

Comments
 (0)