Skip to content

merge_request factory should allow use with let_it_be

The following discussion from !41282 (merged) should be addressed:

  • @terrichu started a discussion: (+1 comment)

    thought (non-blocking): I noticed this when I was working on the tests in another MR. Should we open an issue to see if the merge_request factory can be modified so it will work with let_it_be? I looked but couldn't find an existing issue

When let_it_be is used for a merge_request that has a project also defined by let_it_be, an error is thrown due to this block of code:

      # Fake `fetch_ref!` if we don't have repository
      # We have too many existing tests replying on this behaviour
      unless [target_project, source_project].all?(&:repository_exists?)
        allow(merge_request).to receive(:fetch_ref!)
      end

Stacktrace

 Failure/Error: allow(merge_request).to receive(:fetch_ref!)
       The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported.
     Shared Example Group: "search issues scope filters by state" called from ./ee/spec/lib/gitlab/elastic/group_search_results_spec.rb:33
     # ./spec/factories/merge_requests.rb:267:in `block (3 levels) in <main>'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/callback.rb:13:in `instance_exec'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/callback.rb:13:in `run'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/callbacks_observer.rb:11:in `block in update'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/callbacks_observer.rb:10:in `each'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/callbacks_observer.rb:10:in `update'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/evaluation.rb:24:in `notify'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/strategy/create.rb:10:in `block in result'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/strategy/create.rb:9:in `tap'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/strategy/create.rb:9:in `result'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/factory.rb:43:in `run'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/factory_runner.rb:29:in `block in run'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/activesupport-6.0.3.1/lib/active_support/notifications.rb:182:in `instrument'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/factory_runner.rb:28:in `run'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
     # ./ee/spec/lib/gitlab/elastic/group_search_results_spec.rb:20:in `block (3 levels) in <main>'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/test-prof-0.12.0/lib/test_prof/recipes/rspec/let_it_be.rb:92:in `instance_exec'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/test-prof-0.12.0/lib/test_prof/recipes/rspec/let_it_be.rb:92:in `block in let_it_be'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:318:in `block in load'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:291:in `load_dependency'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:318:in `load'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/nakayoshi_fork-0.0.4/lib/nakayoshi_fork.rb:23:in `fork'
     # /Users/terrichu/.rvm/gems/ruby-2.6.6/gems/nakayoshi_fork-0.0.4/lib/nakayoshi_fork.rb:23:in `fork'
     # -e:1:in `<main>'
Edited by Thong Kuah