Skip to content

Using prepend causes problems when twinned with any_instance rspec expectations

As seen in https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/2108

We tried to move elasticsearch functionality to the EE::ApplicationSetting module in EE, but our specs use any_instance expectations (particularly in stub_application_settings, but there's nearly 900 sites in total), which are incompatible with prepend.

1) ElasticIndexerWorker returns true if ES disabled
     Got 1 failure and 1 other error:

     1.1) Failure/Error: allow_any_instance_of(ApplicationSetting).to receive_messages(messages)
            Using `any_instance` to stub a method (elasticsearch_indexing) that has been defined on a prepended module (EE::ApplicationSetting) is not supported.
          # ./spec/support/stub_configuration.rb:7:in `stub_application_setting'
          # ./spec/workers/elastic_indexer_worker_spec.rb:8:in `block (2 levels) in <top (required)>'
          # /home/lupine/.gem/ruby/2.3.4/gems/rspec-retry-0.4.5/lib/rspec/retry.rb:98:in `block in run'
          # /home/lupine/.gem/ruby/2.3.4/gems/rspec-retry-0.4.5/lib/rspec/retry.rb:88:in `loop'
          # /home/lupine/.gem/ruby/2.3.4/gems/rspec-retry-0.4.5/lib/rspec/retry.rb:88:in `run'
          # /home/lupine/.gem/ruby/2.3.4/gems/rspec-retry-0.4.5/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
          # /home/lupine/.gem/ruby/2.3.4/gems/rspec-retry-0.4.5/lib/rspec/retry.rb:22:in `block (2 levels) in setup'

@yorickpeterse is also having trouble with this elsewhere in the codebase. This blocks us from organising our code as we'd prefer.

What to do? One option is to blacklist any_instance expectations and rework the suite to operate without them. This is possible, but something of a time sink.

do we really really like prepend? Is avoiding it an option?

/cc @DouweM

Edited by Nick Thomas