From 7d3b0113b1e87bbaf4ec239f05057fa41d13f505 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 10 Apr 2024 10:11:22 +0200 Subject: [PATCH] Fix various warnings in the test suite --- lib/active_model/serializer.rb | 3 +- lib/active_model/serializer/association.rb | 2 + test/fixtures/poro.rb | 7 ++-- .../scaffold_controller_generator_test.rb | 40 +++++++++---------- test/test_app.rb | 2 + .../active_model/default_serializer_test.rb | 2 +- .../serializer/url_helpers_test.rb | 1 + 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 29e7a0c9f..f258f6362 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -167,7 +167,8 @@ def initialize(object, options={}) @context = options[:context] @namespace = options[:namespace] end - attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic + attr_accessor :object, :scope, :root, :meta_key, :meta, :context, :polymorphic + attr_writer :key_format def json_key key = if root == true || root.nil? diff --git a/lib/active_model/serializer/association.rb b/lib/active_model/serializer/association.rb index 44313076e..f742c5ec6 100644 --- a/lib/active_model/serializer/association.rb +++ b/lib/active_model/serializer/association.rb @@ -9,6 +9,8 @@ class Serializer class Association def initialize(name, options={}) if options.has_key?(:include) + puts '-' * 40 + puts caller ActiveSupport::Deprecation.warn <<-WARN ** Notice: include was renamed to embed_in_root. ** WARN diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 4539e8401..72f7fee4e 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -144,14 +144,14 @@ class TypeSerializer < ActiveModel::Serializer class SelfReferencingUserParentSerializer < ActiveModel::Serializer attributes :name - has_one :type, serializer: TypeSerializer, embed: :ids, include: true + has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true end class SelfReferencingUserSerializer < ActiveModel::Serializer attributes :name - has_one :type, serializer: TypeSerializer, embed: :ids, include: true - has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, include: true + has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true + has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, embed_in_root: true end class UserInfoSerializer < ActiveModel::Serializer @@ -176,6 +176,7 @@ class CategorySerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer attributes :title, :body + alias_method :title, :title # silence method redefinition warning def title keyword = serialization_options[:highlight_keyword] title = object.read_attribute_for_serialization(:title) diff --git a/test/integration/generators/scaffold_controller_generator_test.rb b/test/integration/generators/scaffold_controller_generator_test.rb index dc6c4c493..d5605cfdb 100644 --- a/test/integration/generators/scaffold_controller_generator_test.rb +++ b/test/integration/generators/scaffold_controller_generator_test.rb @@ -17,44 +17,44 @@ def test_generated_controller assert_file 'app/controllers/accounts_controller.rb' do |content| assert_instance_method :index, content do |m| - assert_match /@accounts = Account\.all/, m - assert_match /format.html/, m - assert_match /format.json \{ render json: @accounts \}/, m + assert_match(/@accounts = Account\.all/, m) + assert_match(/format.html/, m) + assert_match(/format.json \{ render json: @accounts \}/, m) end assert_instance_method :show, content do |m| - assert_match /format.html/, m - assert_match /format.json \{ render json: @account \}/, m + assert_match(/format.html/, m) + assert_match(/format.json \{ render json: @account \}/, m) end assert_instance_method :new, content do |m| - assert_match /@account = Account\.new/, m + assert_match(/@account = Account\.new/, m) end assert_instance_method :edit, content do |m| - assert m.blank? + assert_predicate m, :blank? end assert_instance_method :create, content do |m| - assert_match /@account = Account\.new\(account_params\)/, m - assert_match /@account\.save/, m - assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m - assert_match /format\.json \{ render json: @account, status: :created \}/, m - assert_match /format\.html \{ render action: 'new' \}/, m - assert_match /format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m + assert_match(/@account = Account\.new\(account_params\)/, m) + assert_match(/@account\.save/, m) + assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m) + assert_match(/format\.json \{ render json: @account, status: :created \}/, m) + assert_match(/format\.html \{ render action: 'new' \}/, m) + assert_match(/format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m) end assert_instance_method :update, content do |m| - assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m - assert_match /format\.json \{ head :no_content \}/, m - assert_match /format\.html \{ render action: 'edit' \}/, m - assert_match /format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m + assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m) + assert_match(/format\.json \{ head :no_content \}/, m) + assert_match(/format\.html \{ render action: 'edit' \}/, m) + assert_match(/format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m) end assert_instance_method :destroy, content do |m| - assert_match /@account\.destroy/, m - assert_match /format\.html { redirect_to accounts_url \}/, m - assert_match /format\.json \{ head :no_content \}/, m + assert_match(/@account\.destroy/, m) + assert_match(/format\.html { redirect_to accounts_url \}/, m) + assert_match(/format\.json \{ head :no_content \}/, m) end assert_match(/def account_params/, content) diff --git a/test/test_app.rb b/test/test_app.rb index 3206bffda..51569ef7a 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -1,4 +1,6 @@ class TestApp < Rails::Application + config.load_defaults("#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}") + if Rails.version.to_s.first >= '4' config.eager_load = false config.secret_key_base = 'abc123' diff --git a/test/unit/active_model/default_serializer_test.rb b/test/unit/active_model/default_serializer_test.rb index fb64e5654..5b674c719 100644 --- a/test/unit/active_model/default_serializer_test.rb +++ b/test/unit/active_model/default_serializer_test.rb @@ -4,7 +4,7 @@ module ActiveModel class DefaultSerializer class Test < Minitest::Test def test_serialize_objects - assert_equal(nil, DefaultSerializer.new(nil).serializable_object) + assert_nil(DefaultSerializer.new(nil).serializable_object) assert_equal(1, DefaultSerializer.new(1).serializable_object) assert_equal('hi', DefaultSerializer.new('hi').serializable_object) end diff --git a/test/unit/active_model/serializer/url_helpers_test.rb b/test/unit/active_model/serializer/url_helpers_test.rb index 70bea6457..6d59d81ca 100644 --- a/test/unit/active_model/serializer/url_helpers_test.rb +++ b/test/unit/active_model/serializer/url_helpers_test.rb @@ -21,6 +21,7 @@ def test_url_helpers_are_available serializer = Class.new(ActiveModel::Serializer) do attributes :url + alias_method :url, :url # silence redefinition warning def url profile_url(id: object.object_id) end