From d641800371a60460cd4c34642e40327a2c2cdd80 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Wed, 29 May 2024 11:23:51 -0400 Subject: [PATCH] Create associations directly via the through table Rails 7 changed the way migrations are run. The migration class constant is now removed and the file is reloaded between migrations. This means tests should not use expect/allow on migration stub classes as the constant stubbed is not the same constant the migration is run against. Additionally, comparisons of objects created before the migration with ones after will also not work. In that case, you can assert by id or other attributes instead of comparing objects. Rails also checks association classes are correct and will also not match expectations. For example, instead of using host to create or query the host association, use host_id instead. This also works with has_manys such as miq_product_feature_ids. See https://github.com/rails/rails/pull 42198 --- ...0190201173316_add_missing_ems_id_to_switch_spec.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spec/migrations/20190201173316_add_missing_ems_id_to_switch_spec.rb b/spec/migrations/20190201173316_add_missing_ems_id_to_switch_spec.rb index b635a626..9f8e369f 100644 --- a/spec/migrations/20190201173316_add_missing_ems_id_to_switch_spec.rb +++ b/spec/migrations/20190201173316_add_missing_ems_id_to_switch_spec.rb @@ -34,6 +34,7 @@ class Switch < ActiveRecord::Base let(:switch_stub) { migration_stub(:Switch) } let(:host_stub) { migration_stub(:Host) } let(:ems_stub) { migration_stub(:ExtManagementSystem) } + let(:host_switch_stub) { migration_stub(:HostSwitch) } migration_context :up do it "migrates a series of representative rows" do @@ -73,11 +74,11 @@ class Switch < ActiveRecord::Base :type => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch", :ems_id => nil) # Host -> switches mapping - host_esx.host_switches.create!(:host_id => host_esx.id, :switch_id => dvswitch.id) - host_esx.host_switches.create!(:host_id => host_esx.id, :switch_id => host_switch.id) - host_esx_archived.host_switches.create!(:host_id => host_esx_archived.id, :switch_id => host_switch_archived.id) - host_esx_archived.host_switches.create!(:host_id => host_esx_archived.id, :switch_id => dvswitch_archived.id) - host_redhat.host_switches.create!(:host_id => host_redhat.id, :switch_id => redhat_switch.id) + host_switch_stub.create!(:host_id => host_esx.id, :switch_id => dvswitch.id) + host_switch_stub.create!(:host_id => host_esx.id, :switch_id => host_switch.id) + host_switch_stub.create!(:host_id => host_esx_archived.id, :switch_id => host_switch_archived.id) + host_switch_stub.create!(:host_id => host_esx_archived.id, :switch_id => dvswitch_archived.id) + host_switch_stub.create!(:host_id => host_redhat.id, :switch_id => redhat_switch.id) migrate