Consider removing the use of skip_callback and set_callback

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Problem

In some factories we are using skip_callback (via after(:build) and set_callback to restore (via after(:create)):

Now, if we just build a factory the skipped (read: removed) callback is not restored and this is permanent for the rest of spec suite's lifetime.

Example

Let's look at the JiraService spec but it could be any other as well:

diff --git a/spec/models/project_services/jira_service_spec.rb b/spec/models/project_services/jira_service_spec.rb
index 6da16905363..7efeb13a41f 100644
--- a/spec/models/project_services/jira_service_spec.rb
+++ b/spec/models/project_services/jira_service_spec.rb
@@ -12,6 +12,10 @@ RSpec.describe JiraService do
   let(:transition_id) { 'test27' }
   let(:server_info_results) { { 'deploymentType' => 'Cloud' } }
 
+  before(:all) do
+    build(:jira_service, :without_properties_callback)
+  end
+
   before do
     WebMock.stub_request(:get, /serverInfo/).to_return(body: server_info_results.to_json )
   end

After this line, handle_properties callback is disabled for the rest of the lifetime.

Luckily, we'd get failures in these cases but that might not always be true:

  41) JiraService overriding properties when data are stored in properties behaves like handles jira fields #update basic update leaves properties field emtpy
      Failure/Error: IssueTrackerService.skip_callback(:validation, :before, :handle_properties)

      ArgumentError:
        Before validation callback :handle_properties has not been defined
      Shared Example Group: "handles jira fields" called from ./spec/models/project_services/jira_service_spec.rb:395
      # ./spec/factories/services.rb:190:in `block (3 levels) in <main>'
      # ./spec/models/project_services/jira_service_spec.rb:392:in `block (4 levels) in <main>'
      # ./spec/spec_helper.rb:326:in `block (3 levels) in <main>'
      # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
      # ./spec/spec_helper.rb:317:in `block (2 levels) in <main>'
      # ./spec/spec_helper.rb:313:in `block (3 levels) in <main>'
      # ./spec/spec_helper.rb:313:in `block (2 levels) in <main>'
      # -e:1:in `<main>'

Proposed solution

Consider removing and banning (via 👮) the use of skip_callback and set_callback in spec factories, specs or even in complete codebase.

As an alternative, we could try to use ActiveRecord's custom context. See https://guides.rubyonrails.org/active_record_validations.html#on.

Edited by 🤖 GitLab Bot 🤖