Commit 2132a863 authored by Rémy Coutable's avatar Rémy Coutable

Fix and document an RSpec::Parameterized::TableSyntax edge-case

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 1abf34cb
...@@ -358,16 +358,11 @@ range of inputs, might look like this: ...@@ -358,16 +358,11 @@ range of inputs, might look like this:
describe "#==" do describe "#==" do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let(:project1) { create(:project) }
let(:project2) { create(:project) }
where(:a, :b, :result) do where(:a, :b, :result) do
1 | 1 | true 1 | 1 | true
1 | 2 | false 1 | 2 | false
true | true | true true | true | true
true | false | false true | false | false
project1 | project1 | true
project2 | project2 | true
project 1 | project2 | false
end end
with_them do with_them do
...@@ -380,6 +375,11 @@ describe "#==" do ...@@ -380,6 +375,11 @@ describe "#==" do
end 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 tests
Prometheus metrics may be preserved from one test run to another. To ensure that metrics are Prometheus metrics may be preserved from one test run to another. To ensure that metrics are
......
...@@ -46,15 +46,15 @@ describe Vulnerabilities::Occurrence do ...@@ -46,15 +46,15 @@ describe Vulnerabilities::Occurrence do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
# we use block to delay object creations # we use block to delay object creations
where(:key, :value_block) do where(:key, :factory_name) do
:primary_identifier | -> { create(:vulnerabilities_identifier) } :primary_identifier | :vulnerabilities_identifier
:scanner | -> { create(:vulnerabilities_scanner) } :scanner | :vulnerabilities_scanner
:project | -> { create(:project) } :project | :project
end end
with_them do with_them do
it "is valid" do it "is valid" do
expect { new_occurrence.update!({ key => value_block.call }) }.not_to raise_error expect { new_occurrence.update!({ key => create(factory_name) }) }.not_to raise_error
end end
end end
end end
......
...@@ -4,7 +4,6 @@ require 'spec_helper' ...@@ -4,7 +4,6 @@ require 'spec_helper'
describe PrometheusMetric do describe PrometheusMetric do
subject { build(:prometheus_metric) } subject { build(:prometheus_metric) }
let(:other_project) { build(:project) }
it_behaves_like 'having unique enum values' it_behaves_like 'having unique enum values'
...@@ -16,17 +15,17 @@ describe PrometheusMetric do ...@@ -16,17 +15,17 @@ describe PrometheusMetric do
describe 'common metrics' do describe 'common metrics' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:common, :project, :result) do where(:common, :with_project, :result) do
false | other_project | true false | true | true
false | nil | false false | false | false
true | other_project | false true | true | false
true | nil | true true | false | true
end end
with_them do with_them do
before do before do
subject.common = common subject.common = common
subject.project = project subject.project = with_project ? build(:project) : nil
end end
it { expect(subject.valid?).to eq(result) } it { expect(subject.valid?).to eq(result) }
......
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