Skip to content

Commit

Permalink
Fix test suite deprecations caused by Rails 7.1 (#875)
Browse files Browse the repository at this point in the history
Fix warnings related to ActionController::Parameters
  • Loading branch information
tomascco authored Oct 7, 2023
1 parent 204e34a commit 82ba4b3
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 45 deletions.
16 changes: 10 additions & 6 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def method_missing(symbol, *arguments, &block)
user
end
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class IndexActionBaseTest < ActionController::TestCase
Expand Down Expand Up @@ -181,21 +185,21 @@ class CreateActionBaseTest < ActionController::TestCase
include UserTestHelper

def test_expose_a_newly_create_user_when_saved_with_success
User.expects(:new).with({'these' => 'params'}).returns(mock_user(save: true))
User.expects(:new).with(build_parameters({'these' => 'params'})).returns(mock_user(save: true))
post :create, params: { user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end

def test_expose_a_newly_create_user_when_saved_with_success_and_role_setted
@controller.class.send(:with_role, :admin)
User.expects(:new).with({'these' => 'params'}, {as: :admin}).returns(mock_user(save: true))
User.expects(:new).with(build_parameters({'these' => 'params'}), {as: :admin}).returns(mock_user(save: true))
post :create, params: { user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end

def test_expose_a_newly_create_user_when_saved_with_success_and_without_protection_setted
@controller.class.send(:without_protection, true)
User.expects(:new).with({'these' => 'params'}, {without_protection: true}).returns(mock_user(save: true))
User.expects(:new).with(build_parameters({'these' => 'params'}), {without_protection: true}).returns(mock_user(save: true))
post :create, params: { user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end
Expand Down Expand Up @@ -238,23 +242,23 @@ class UpdateActionBaseTest < ActionController::TestCase

def test_update_the_requested_object
User.expects(:find).with('42').returns(mock_user)
mock_user.expects(:update).with({'these' => 'params'}).returns(true)
mock_user.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: '42', user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end

def test_update_the_requested_object_when_setted_role
@controller.class.send(:with_role, :admin)
User.expects(:find).with('42').returns(mock_user)
mock_user.expects(:update).with({'these' => 'params'}, {as: :admin}).returns(true)
mock_user.expects(:update).with(build_parameters({'these' => 'params'}), {as: :admin}).returns(true)
put :update, params: { id: '42', user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end

def test_update_the_requested_object_when_setted_without_protection
@controller.class.send(:without_protection, true)
User.expects(:find).with('42').returns(mock_user)
mock_user.expects(:update).with({'these' => 'params'}, {without_protection: true}).returns(true)
mock_user.expects(:update).with(build_parameters({'these' => 'params'}), {without_protection: true}).returns(true)
put :update, params: { id: '42', user: {these: 'params'} }
assert_equal mock_user, assigns(:user)
end
Expand Down
14 changes: 11 additions & 3 deletions test/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def test_expose_the_requested_comment_on_edit
end

def test_expose_a_newly_create_comment_on_create
Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(save: true))
Comment.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_comment(save: true))
post :create, params: { post_id: '37', comment: {these: 'params'} }
assert_equal mock_post, assigns(:post)
assert_equal mock_comment, assigns(:comment)
end

def test_update_the_requested_object_on_update
Comment.expects(:find).with('42').returns(mock_comment)
mock_comment.expects(:update).with({'these' => 'params'}).returns(true)
mock_comment.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: '42', post_id: '37', comment: {these: 'params'} }
assert_equal mock_post, assigns(:post)
assert_equal mock_comment, assigns(:comment)
Expand Down Expand Up @@ -103,6 +103,10 @@ def mock_post(stubs={})
def mock_comment(stubs={})
@mock_comment ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class Reply
Expand Down Expand Up @@ -132,7 +136,7 @@ def teardown

def test_redirect_to_the_post_on_create_if_show_and_index_undefined
@controller.expects(:parent_url).returns('http://test.host/')
Reply.expects(:build).with({'these' => 'params'}).returns(mock_reply(save: true))
Reply.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_reply(save: true))
post :create, params: { post_id: '37', reply: { these: 'params' } }
assert_redirected_to 'http://test.host/'
end
Expand Down Expand Up @@ -161,4 +165,8 @@ def mock_post(stubs={})
def mock_reply(stubs={})
@mock_reply ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end
8 changes: 6 additions & 2 deletions test/belongs_to_with_shallow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_expose_a_new_tag_on_new
end

def test_expose_a_newly_create_tag_on_create
Tag.expects(:build).with({'these' => 'params'}).returns(mock_tag(save: true))
Tag.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_tag(save: true))
post :create, params: { post_id: 'thirty_seven', tag: {these: 'params'} }
assert_equal mock_post, assigns(:post)
assert_equal mock_tag, assigns(:tag)
Expand All @@ -67,7 +67,7 @@ def test_expose_the_requested_tag_on_edit

def test_update_the_requested_object_on_update
should_find_parents
mock_tag.expects(:update).with({'these' => 'params'}).returns(true)
mock_tag.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: '42', tag: {these: 'params'} }
assert_equal mock_post, assigns(:post)
assert_equal mock_tag, assigns(:tag)
Expand Down Expand Up @@ -96,4 +96,8 @@ def mock_post(stubs={})
def mock_tag(stubs={})
@mock_tag ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end
8 changes: 6 additions & 2 deletions test/customized_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def mock_car(expectations={})
car
end
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class IndexActionCustomizedBaseTest < ActionController::TestCase
Expand Down Expand Up @@ -115,7 +119,7 @@ class CreateActionCustomizedBaseTest < ActionController::TestCase
include CarTestHelper

def test_expose_a_newly_create_user_when_saved_with_success
Car.expects(:create_new).with({'these' => 'params'}).returns(mock_car(save_successfully: true))
Car.expects(:create_new).with(build_parameters({'these' => 'params'})).returns(mock_car(save_successfully: true))
post :create, params: { car: {these: 'params'} }
assert_equal mock_car, assigns(:car)
end
Expand All @@ -140,7 +144,7 @@ class UpdateActionCustomizedBaseTest < ActionController::TestCase

def test_update_the_requested_object
Car.expects(:get).with('42').returns(mock_car)
mock_car.expects(:update_successfully).with({'these' => 'params'}).returns(true)
mock_car.expects(:update_successfully).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: '42', car: {these: 'params'} }
assert_equal mock_car, assigns(:car)
end
Expand Down
16 changes: 12 additions & 4 deletions test/defaults_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def test_expose_the_requested_painter_on_edit
end

def test_expose_a_newly_create_painter_when_saved_with_success
Malarz.expects(:new).with({'these' => 'params'}).returns(mock_painter(save: true))
Malarz.expects(:new).with(build_parameters({'these' => 'params'})).returns(mock_painter(save: true))
post :create, params: { malarz: {these: 'params'} }
assert_equal mock_painter, assigns(:malarz)
end

def test_update_the_requested_object
Malarz.expects(:find_by_slug).with('forty_two').returns(mock_painter)
mock_painter.expects(:update).with({'these' => 'params'}).returns(true)
mock_painter.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: 'forty_two', malarz: {these: 'params'} }
assert_equal mock_painter, assigns(:malarz)
end
Expand All @@ -77,6 +77,10 @@ def test_the_requested_painter_is_destroyed
def mock_painter(stubs={})
@mock_painter ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class Lecturer
Expand Down Expand Up @@ -128,14 +132,14 @@ def test_expose_the_requested_lecturer_on_edit
end

def test_expose_a_newly_create_lecturer_when_saved_with_success
Lecturer.expects(:new).with({'these' => 'params'}).returns(mock_lecturer(save: true))
Lecturer.expects(:new).with(build_parameters({'these' => 'params'})).returns(mock_lecturer(save: true))
post :create, params: { lecturer: {these: 'params'} }
assert_equal mock_lecturer, assigns(:lecturer)
end

def test_update_the_lecturer
Lecturer.expects(:find_by_slug).with('forty_two').returns(mock_lecturer)
mock_lecturer.expects(:update).with({'these' => 'params'}).returns(true)
mock_lecturer.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
put :update, params: { id: 'forty_two', lecturer: {these: 'params'} }
assert_equal mock_lecturer, assigns(:lecturer)
end
Expand All @@ -152,6 +156,10 @@ def test_the_requested_lecturer_is_destroyed
def mock_lecturer(stubs={})
@mock_lecturer ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class Group
Expand Down
22 changes: 13 additions & 9 deletions test/multiple_nested_optional_belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_expose_the_requested_project_for_edition_without_parents
def test_expose_a_newly_created_project_with_student
Student.expects(:find).with('37').returns(mock_student)
mock_student.expects(:projects).returns(Project)
Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(save: true))
Project.expects(:build).with(build_parameters({ 'these' => 'params' })).returns(mock_project(save: true))
post :create, params: { student_id: '37', project: { these: 'params' } }
assert_equal mock_student, assigns(:student)
assert_equal mock_project, assigns(:project)
Expand All @@ -225,7 +225,7 @@ def test_expose_a_newly_created_project_with_student
def test_expose_a_newly_created_project_with_manager
Manager.expects(:find).with('37').returns(mock_manager)
mock_manager.expects(:projects).returns(Project)
Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(save: true))
Project.expects(:build).with(build_parameters({ 'these' => 'params' })).returns(mock_project(save: true))
post :create, params: { manager_id: '37', project: { these: 'params' } }
assert_equal mock_manager, assigns(:manager)
assert_equal mock_project, assigns(:project)
Expand All @@ -234,7 +234,7 @@ def test_expose_a_newly_created_project_with_manager
def test_expose_a_newly_created_project_with_employee
Employee.expects(:find).with('37').returns(mock_employee)
mock_employee.expects(:projects).returns(Project)
Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(save: true))
Project.expects(:build).with(build_parameters({ 'these' => 'params' })).returns(mock_project(save: true))
post :create, params: { employee_id: '37', project: { these: 'params' } }
assert_equal mock_employee, assigns(:employee)
assert_equal mock_project, assigns(:project)
Expand All @@ -245,15 +245,15 @@ def test_expose_a_newly_created_project_with_manager_and_employee
mock_manager.expects(:employees).returns(Employee)
Employee.expects(:find).with('42').returns(mock_employee)
mock_employee.expects(:projects).returns(Project)
Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(save: true))
Project.expects(:build).with(build_parameters({ 'these' => 'params' })).returns(mock_project(save: true))
post :create, params: { manager_id: '37', employee_id: '42', project: { these: 'params' } }
assert_equal mock_manager, assigns(:manager)
assert_equal mock_employee, assigns(:employee)
assert_equal mock_project, assigns(:project)
end

def test_expose_a_newly_created_project_without_parents
Project.expects(:new).with({ 'these' => 'params' }).returns(mock_project(save: true))
Project.expects(:new).with(build_parameters({ 'these' => 'params' })).returns(mock_project(save: true))
post :create, params: { project: { these: 'params' } }
assert_equal mock_project, assigns(:project)
end
Expand All @@ -263,7 +263,7 @@ def test_update_the_requested_project_with_student
Student.expects(:find).with('37').returns(mock_student)
mock_student.expects(:projects).returns(Project)
Project.expects(:find).with('42').returns(mock_project)
mock_project.expects(:update).with({ 'these' => 'params' }).returns(true)
mock_project.expects(:update).with(build_parameters({ 'these' => 'params' })).returns(true)
put :update, params: { id: '42', student_id: '37', project: { these: 'params' } }
assert_equal mock_student, assigns(:student)
assert_equal mock_project, assigns(:project)
Expand All @@ -273,7 +273,7 @@ def test_update_the_requested_project_with_manager
Manager.expects(:find).with('37').returns(mock_manager)
mock_manager.expects(:projects).returns(Project)
Project.expects(:find).with('42').returns(mock_project)
mock_project.expects(:update).with({ 'these' => 'params' }).returns(true)
mock_project.expects(:update).with(build_parameters({ 'these' => 'params' })).returns(true)
put :update, params: { id: '42', manager_id: '37', project: { these: 'params' } }
assert_equal mock_manager, assigns(:manager)
assert_equal mock_project, assigns(:project)
Expand All @@ -283,7 +283,7 @@ def test_update_the_requested_project_with_employee
Employee.expects(:find).with('37').returns(mock_employee)
mock_employee.expects(:projects).returns(Project)
Project.expects(:find).with('42').returns(mock_project)
mock_project.expects(:update).with({ 'these' => 'params' }).returns(true)
mock_project.expects(:update).with(build_parameters({ 'these' => 'params' })).returns(true)
put :update, params: { id: '42', employee_id: '37', project: { these: 'params' } }
assert_equal mock_employee, assigns(:employee)
assert_equal mock_project, assigns(:project)
Expand All @@ -295,7 +295,7 @@ def test_update_the_requested_project_with_manager_and_employee
Employee.expects(:find).with('13').returns(mock_employee)
mock_employee.expects(:projects).returns(Project)
Project.expects(:find).with('42').returns(mock_project)
mock_project.expects(:update).with({ 'these' => 'params' }).returns(true)
mock_project.expects(:update).with(build_parameters({ 'these' => 'params' })).returns(true)
put :update, params: { id: '42', manager_id: '37', employee_id: '13', project: { these: 'params' } }
assert_equal mock_manager, assigns(:manager)
assert_equal mock_employee, assigns(:employee)
Expand Down Expand Up @@ -375,4 +375,8 @@ def mock_student(stubs={})
def mock_project(stubs={})
@mock_project ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end
6 changes: 5 additions & 1 deletion test/nested_belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_assigns_country_and_state_and_city_on_edit
end

def test_assigns_country_and_state_and_city_on_create
City.expects(:build).with({'these' => 'params'}).returns(mock_city)
City.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_city)
mock_city.expects(:save).returns(true)
post :create, params: { state_id: '37', country_id: '13', city: {these: 'params'} }

Expand Down Expand Up @@ -113,4 +113,8 @@ def mock_state(stubs={})
def mock_city(stubs={})
@mock_city ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end
6 changes: 5 additions & 1 deletion test/nested_belongs_to_with_shallow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_assigns_dresser_and_shelf_and_plate_on_edit
def test_assigns_dresser_and_shelf_and_plate_on_create
Shelf.expects(:find).with('37').twice.returns(mock_shelf)

Plate.expects(:build).with({'these' => 'params'}).returns(mock_plate)
Plate.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_plate)
mock_plate.expects(:save).returns(true)
post :create, params: { shelf_id: '37', plate: {these: 'params'} }

Expand Down Expand Up @@ -126,4 +126,8 @@ def mock_shelf(stubs={})
def mock_plate(stubs={})
@mock_plate ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end
8 changes: 6 additions & 2 deletions test/nested_model_with_shallow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ def test_assigns_subfaculty_and_speciality_and_group_on_edit

def test_expose_a_newly_create_group_with_speciality
Speciality.expects(:find).with('37').twice.returns(mock_speciality)
Plan::Group.expects(:build).with({'these' => 'params'}).returns(mock_group(save: true))
Plan::Group.expects(:build).with(build_parameters({'these' => 'params'})).returns(mock_group(save: true))
post :create, params: { speciality_id: '37', group: {'these' => 'params'} }
assert_equal mock_group, assigns(:group)
end

def test_expose_a_update_group_with_speciality
should_find_parents
mock_group.expects(:update).with('these' => 'params').returns(true)
mock_group.expects(:update).with(build_parameters({'these' => 'params'})).returns(true)
post :update, params: { id: 'forty_two', group: {'these' => 'params'} }
assert_equal mock_group, assigns(:group)
end
Expand All @@ -97,6 +97,10 @@ def mock_speciality(stubs={})
def mock_subfaculty(stubs={})
@mock_subfaculty ||= mock(stubs)
end

def build_parameters(hash)
ActionController::Parameters.new(hash)
end
end

class TwoNestedModelWithShallowTest < ActionController::TestCase
Expand Down
Loading

0 comments on commit 82ba4b3

Please sign in to comment.