Commit f5114783 authored by Pawel Chojnacki's avatar Pawel Chojnacki

use define_method to define devine_metric_method. (couldn't resist)

parent a760445f
...@@ -8,17 +8,15 @@ describe Gitlab::Metrics::Concern do ...@@ -8,17 +8,15 @@ describe Gitlab::Metrics::Concern do
let(:metric_name) { :sample_metric } let(:metric_name) { :sample_metric }
describe "#define_#{metric_type}" do describe "#define_#{metric_type}" do
let(:_metric_type) { metric_type } define_method(:call_define_metric_method) do |**args|
subject.__send__("define_#{metric_type}", metric_name, **args)
def define_metric_method(**args)
subject.send("define_#{_metric_type}", metric_name, **args)
end end
context 'metrics access method not defined' do context 'metrics access method not defined' do
it "defines metrics accessing method" do it "defines metrics accessing method" do
expect(subject).not_to respond_to(metric_name) expect(subject).not_to respond_to(metric_name)
define_metric_method(docstring: docstring) call_define_metric_method(docstring: docstring)
expect(subject).to respond_to(metric_name) expect(subject).to respond_to(metric_name)
end end
...@@ -26,11 +24,11 @@ describe Gitlab::Metrics::Concern do ...@@ -26,11 +24,11 @@ describe Gitlab::Metrics::Concern do
context 'metrics access method defined' do context 'metrics access method defined' do
before do before do
define_metric_method(docstring: docstring) call_define_metric_method(docstring: docstring)
end end
it 'raises error when trying to redefine method' do it 'raises error when trying to redefine method' do
expect { define_metric_method(docstring: docstring) }.to raise_error(ArgumentError) expect { call_define_metric_method(docstring: docstring) }.to raise_error(ArgumentError)
end end
context 'metric is not cached' do context 'metric is not cached' do
...@@ -56,18 +54,17 @@ describe Gitlab::Metrics::Concern do ...@@ -56,18 +54,17 @@ describe Gitlab::Metrics::Concern do
end end
describe "#fetch_#{metric_type}" do describe "#fetch_#{metric_type}" do
let(:_metric_type) { metric_type }
let(:null_metric) { Gitlab::Metrics::NullMetric.instance } let(:null_metric) { Gitlab::Metrics::NullMetric.instance }
def fetch_metric_method(**args) define_method(:call_fetch_metric_method) do |**args|
subject.send("fetch_#{_metric_type}", metric_name, **args) subject.__send__("fetch_#{metric_type}", metric_name, **args)
end end
context "when #{metric_type} is not cached" do context "when #{metric_type} is not cached" do
it 'initializes counter metric' do it 'initializes counter metric' do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric) allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
fetch_metric_method(docstring: docstring) call_fetch_metric_method(docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args) expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end end
...@@ -75,13 +72,13 @@ describe Gitlab::Metrics::Concern do ...@@ -75,13 +72,13 @@ describe Gitlab::Metrics::Concern do
context "when #{metric_type} is cached" do context "when #{metric_type} is cached" do
before do before do
fetch_metric_method(docstring: docstring) call_fetch_metric_method(docstring: docstring)
end end
it 'uses class metric cache' do it 'uses class metric cache' do
expect(Gitlab::Metrics).not_to receive(metric_type) expect(Gitlab::Metrics).not_to receive(metric_type)
fetch_metric_method(docstring: docstring) call_fetch_metric_method(docstring: docstring)
end end
context 'when metric is reloaded' do context 'when metric is reloaded' do
...@@ -92,7 +89,7 @@ describe Gitlab::Metrics::Concern do ...@@ -92,7 +89,7 @@ describe Gitlab::Metrics::Concern do
it "initializes #{metric_type} metric" do it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric) allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
fetch_metric_method(docstring: docstring) call_fetch_metric_method(docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args) expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end end
...@@ -101,7 +98,7 @@ describe Gitlab::Metrics::Concern do ...@@ -101,7 +98,7 @@ describe Gitlab::Metrics::Concern do
context 'when metric is configured with feature' do context 'when metric is configured with feature' do
let(:feature_name) { :some_metric_feature } let(:feature_name) { :some_metric_feature }
let(:metric) { fetch_metric_method(docstring: docstring, with_feature: feature_name) } let(:metric) { call_fetch_metric_method(docstring: docstring, with_feature: feature_name) }
context 'when feature is enabled' do context 'when feature is enabled' do
before do before do
......
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