Commit 45ab261f authored by Nicolas Dular's avatar Nicolas Dular

Fix Snowplow spec helper

With Ruby 2.6 using `**kwargs` here will not work because it would be an
empty hash that we then expect to be submitted to
`Gitlab::Tracking#event`.

An example when using `expect_snowplow_event`:

```
expect_snowplow_event(
  category: 'cluster:services:prometheus',
  action: 'enabled_manual_prometheus'
)
```

Error:
```
Gitlab::Tracking received :event with unexpected arguments
expected:
  ("cluster:services:prometheus", "enabled_manual_prometheus", {})
got:
  ("cluster:services:prometheus", "disabled_manual_prometheus") (1 time)
("cluster:services:prometheus", "enabled_manual_prometheus") (1 time)
```

This has not been an issue so far because the other specs we migrated
always provided additional `kwargs`.

To prevent this, we only expect the `**kwargs` parameter when it exists.
However, this will get fixed with Ruby 2.7 and we can remove the
condition again.
parent 17faea01
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching do
RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching, :snowplow do
include PrometheusHelpers
include ReactiveCachingHelpers
......@@ -421,18 +421,16 @@ RSpec.describe PrometheusService, :use_clean_rails_memory_store_caching do
context "enabling manual_configuration" do
it "tracks enable event" do
service.update!(manual_configuration: false)
expect(Gitlab::Tracking).to receive(:event).with('cluster:services:prometheus', 'enabled_manual_prometheus')
service.update!(manual_configuration: true)
expect_snowplow_event(category: 'cluster:services:prometheus', action: 'enabled_manual_prometheus')
end
it "tracks disable event" do
service.update!(manual_configuration: true)
expect(Gitlab::Tracking).to receive(:event).with('cluster:services:prometheus', 'disabled_manual_prometheus')
service.update!(manual_configuration: false)
expect_snowplow_event(category: 'cluster:services:prometheus', action: 'disabled_manual_prometheus')
end
end
end
......
......@@ -32,8 +32,16 @@ module SnowplowHelpers
# end
# end
def expect_snowplow_event(category:, action:, **kwargs)
expect(Gitlab::Tracking).to have_received(:event)
.with(category, action, **kwargs).at_least(:once)
# This check will no longer be needed with Ruby 2.7 which
# would not pass any arguments when using **kwargs.
# https://gitlab.com/gitlab-org/gitlab/-/issues/263430
if kwargs.present?
expect(Gitlab::Tracking).to have_received(:event)
.with(category, action, **kwargs).at_least(:once)
else
expect(Gitlab::Tracking).to have_received(:event)
.with(category, action).at_least(:once)
end
end
# Asserts that no call to `Gitlab::Tracking#event` was made.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment