Skip to content

Commit 30734bf

Browse files
bf4GregPK
authored andcommitted
Improve tests found by assert_nil
1 parent 4071935 commit 30734bf

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

test/action_controller/serialization_scope_name_test.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def json_key
3333
end
3434
end
3535
class PostTestController < ActionController::Base
36-
attr_accessor :current_user
36+
attr_writer :current_user
37+
3738
def render_post_by_non_admin
3839
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
3940
render json: new_post, serializer: serializer, adapter: :json
@@ -44,6 +45,10 @@ def render_post_by_admin
4445
render json: new_post, serializer: serializer, adapter: :json
4546
end
4647

48+
def current_user
49+
defined?(@current_user) ? @current_user : :current_user_not_set
50+
end
51+
4752
private
4853

4954
def new_post
@@ -75,10 +80,8 @@ def test_default_serialization_scope
7580
end
7681

7782
def test_default_serialization_scope_object
78-
expected = @controller.current_user
79-
actual = @controller.serialization_scope
80-
assert_nil expected
81-
assert_nil actual
83+
assert_equal :current_user_not_set, @controller.current_user
84+
assert_equal :current_user_not_set, @controller.serialization_scope
8285
end
8386

8487
def test_default_scope_non_admin
@@ -128,7 +131,7 @@ def test_defined_serialization_scope
128131
end
129132

130133
def test_defined_serialization_scope_object
131-
assert_equal @controller.view_context.class, @controller.serialization_scope.class
134+
assert_equal @controller.view_context.controller, @controller.serialization_scope.controller
132135
end
133136

134137
def test_serialization_scope_non_admin

test/active_model_serializers/railtie_test_isolated.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
class RailtieTest < ActiveSupport::TestCase
55
include ActiveSupport::Testing::Isolation
66

7-
class WithRails < RailtieTest
7+
class WithRailsRequiredFirst < RailtieTest
88
setup do
99
require 'rails'
1010
require 'active_model_serializers'
11-
make_basic_app
11+
make_basic_app do |app|
12+
app.config.action_controller.perform_caching = true
13+
end
1214
end
1315

1416
test 'mixes ActionController::Serialization into ActionController::Base' do
@@ -32,17 +34,17 @@ class WithRails < RailtieTest
3234

3335
test 'it is configured for caching' do
3436
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
35-
expected = Rails.configuration.action_controller.perform_caching
36-
actual = ActiveModelSerializers.config.perform_caching
37-
assert_nil expected
38-
assert_nil actual
37+
assert_equal true, Rails.configuration.action_controller.perform_caching
38+
assert_equal true, ActiveModelSerializers.config.perform_caching
3939
end
4040
end
4141

42-
class WithoutRails < RailtieTest
42+
class WithoutRailsRequiredFirst < RailtieTest
4343
setup do
4444
require 'active_model_serializers'
45-
make_basic_app
45+
make_basic_app do |app|
46+
app.config.action_controller.perform_caching = true
47+
end
4648
end
4749

4850
test 'does not mix ActionController::Serialization into ActionController::Base' do
@@ -59,8 +61,8 @@ class WithoutRails < RailtieTest
5961
test 'it is not configured for caching' do
6062
refute_nil ActionController::Base.cache_store
6163
assert_nil ActiveModelSerializers.config.cache_store
62-
refute Rails.configuration.action_controller.perform_caching
63-
refute ActiveModelSerializers.config.perform_caching
64+
assert_equal true, Rails.configuration.action_controller.perform_caching
65+
assert_nil ActiveModelSerializers.config.perform_caching
6466
end
6567
end
6668
end

0 commit comments

Comments
 (0)