From b85b6590e50ac8ae6fa2fda64a58dc83f6ca615f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 20 Feb 2019 19:51:26 +0100 Subject: Fix and document an RSpec::Parameterized::TableSyntax edge-case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- doc/development/testing_guide/best_practices.md | 10 +++++----- spec/models/prometheus_metric_spec.rb | 13 ++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 4cc3812b0f0..2c8d488877b 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -358,16 +358,11 @@ range of inputs, might look like this: describe "#==" do using RSpec::Parameterized::TableSyntax - let(:project1) { create(:project) } - let(:project2) { create(:project) } where(:a, :b, :result) do 1 | 1 | true 1 | 2 | false true | true | true true | false | false - project1 | project1 | true - project2 | project2 | true - project 1 | project2 | false end with_them do @@ -380,6 +375,11 @@ describe "#==" do end ``` +CAUTION: **Caution:** +Only use simple values as input in the `where` block. Using procs, stateful +objects, FactoryBot-created objects etc. can lead to +[unexpected results](https://github.com/tomykaira/rspec-parameterized/issues/8). + ### Prometheus tests Prometheus metrics may be preserved from one test run to another. To ensure that metrics are diff --git a/spec/models/prometheus_metric_spec.rb b/spec/models/prometheus_metric_spec.rb index 2b978c1c8ff..3610408c138 100644 --- a/spec/models/prometheus_metric_spec.rb +++ b/spec/models/prometheus_metric_spec.rb @@ -4,7 +4,6 @@ require 'spec_helper' describe PrometheusMetric do subject { build(:prometheus_metric) } - let(:other_project) { build(:project) } it_behaves_like 'having unique enum values' @@ -16,17 +15,17 @@ describe PrometheusMetric do describe 'common metrics' do using RSpec::Parameterized::TableSyntax - where(:common, :project, :result) do - false | other_project | true - false | nil | false - true | other_project | false - true | nil | true + where(:common, :with_project, :result) do + false | true | true + false | false | false + true | true | false + true | false | true end with_them do before do subject.common = common - subject.project = project + subject.project = with_project ? build(:project) : nil end it { expect(subject.valid?).to eq(result) } -- cgit v1.2.3