Skip to content

Commit bd36637

Browse files
author
Robert Speicher
committed
Merge branch '35804-document-table-based-tests' into 'master'
Use rspec-parameterized for table-based tests Closes #35804 See merge request !13469
2 parents b5bc0fc + caa498f commit bd36637

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ group :development, :test do
324324
gem 'spinach-rerun-reporter', '~> 0.0.2'
325325
gem 'rspec_profiling', '~> 0.0.5'
326326
gem 'rspec-set', '~> 0.1.3'
327+
gem 'rspec-parameterized'
327328

328329
# Prevent occasions where minitest is not bundled in packaged versions of ruby (see #3826)
329330
gem 'minitest', '~> 5.7.0'

Gemfile.lock

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
RedCloth (4.3.2)
5+
abstract_type (0.0.7)
56
ace-rails-ap (4.1.2)
67
actionmailer (4.2.8)
78
actionpack (= 4.2.8)
@@ -41,6 +42,9 @@ GEM
4142
tzinfo (~> 1.1)
4243
acts-as-taggable-on (4.0.0)
4344
activerecord (>= 4.0)
45+
adamantium (0.2.0)
46+
ice_nine (~> 0.11.0)
47+
memoizable (~> 0.4.0)
4448
addressable (2.3.8)
4549
after_commit_queue (1.3.0)
4650
activerecord (>= 3.0)
@@ -124,6 +128,9 @@ GEM
124128
coercible (1.0.0)
125129
descendants_tracker (~> 0.0.1)
126130
colorize (0.7.7)
131+
concord (0.1.5)
132+
adamantium (~> 0.2.0)
133+
equalizer (~> 0.0.9)
127134
concurrent-ruby (1.0.5)
128135
concurrent-ruby-ext (1.0.5)
129136
concurrent-ruby (= 1.0.5)
@@ -470,6 +477,8 @@ GEM
470477
mime-types (>= 1.16, < 4)
471478
mail_room (0.9.1)
472479
memoist (0.15.0)
480+
memoizable (0.4.2)
481+
thread_safe (~> 0.3, >= 0.3.1)
473482
method_source (0.8.2)
474483
mime-types (2.99.3)
475484
mimemagic (0.3.0)
@@ -610,6 +619,11 @@ GEM
610619
premailer-rails (1.9.7)
611620
actionmailer (>= 3, < 6)
612621
premailer (~> 1.7, >= 1.7.9)
622+
proc_to_ast (0.1.0)
623+
coderay
624+
parser
625+
unparser
626+
procto (0.0.3)
613627
prometheus-client-mmap (0.7.0.beta11)
614628
mmap2 (~> 2.2, >= 2.2.7)
615629
pry (0.10.4)
@@ -718,6 +732,10 @@ GEM
718732
chunky_png
719733
rqrcode-rails3 (0.1.7)
720734
rqrcode (>= 0.4.2)
735+
rspec (3.6.0)
736+
rspec-core (~> 3.6.0)
737+
rspec-expectations (~> 3.6.0)
738+
rspec-mocks (~> 3.6.0)
721739
rspec-core (3.6.0)
722740
rspec-support (~> 3.6.0)
723741
rspec-expectations (3.6.0)
@@ -726,6 +744,12 @@ GEM
726744
rspec-mocks (3.6.0)
727745
diff-lcs (>= 1.2.0, < 2.0)
728746
rspec-support (~> 3.6.0)
747+
rspec-parameterized (0.4.0)
748+
binding_of_caller
749+
parser
750+
proc_to_ast
751+
rspec (>= 2.13, < 4)
752+
unparser
729753
rspec-rails (3.6.0)
730754
actionpack (>= 3.0)
731755
activesupport (>= 3.0)
@@ -892,6 +916,14 @@ GEM
892916
get_process_mem (~> 0)
893917
unicorn (>= 4, < 6)
894918
uniform_notifier (1.10.0)
919+
unparser (0.2.6)
920+
abstract_type (~> 0.0.7)
921+
adamantium (~> 0.2.0)
922+
concord (~> 0.1.5)
923+
diff-lcs (~> 1.3)
924+
equalizer (~> 0.0.9)
925+
parser (>= 2.3.1.2, < 2.5)
926+
procto (~> 0.0.2)
895927
url_safe_base64 (0.2.2)
896928
validates_hostname (1.0.6)
897929
activerecord (>= 3.0)
@@ -1094,6 +1126,7 @@ DEPENDENCIES
10941126
responders (~> 2.0)
10951127
rouge (~> 2.0)
10961128
rqrcode-rails3 (~> 0.1.7)
1129+
rspec-parameterized
10971130
rspec-rails (~> 3.6.0)
10981131
rspec-retry (~> 0.4.5)
10991132
rspec-set (~> 0.1.3)

doc/development/testing.md

+37
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,43 @@ end
279279
- Avoid scenario titles that add no information, such as "successfully".
280280
- Avoid scenario titles that repeat the feature title.
281281

282+
### Table-based / Parameterized tests
283+
284+
This style of testing is used to exercise one piece of code with a comprehensive
285+
range of inputs. By specifying the test case once, alongside a table of inputs
286+
and the expected output for each, your tests can be made easier to read and more
287+
compact.
288+
289+
We use the [rspec-parameterized](https://github.com/tomykaira/rspec-parameterized)
290+
gem. A short example, using the table syntax and checking Ruby equality for a
291+
range of inputs, might look like this:
292+
293+
```ruby
294+
describe "#==" do
295+
using Rspec::Parameterized::TableSyntax
296+
297+
let(:project1) { create(:project) }
298+
let(:project2) { create(:project) }
299+
where(:a, :b, :result) do
300+
1 | 1 | true
301+
1 | 2 | false
302+
true | true | true
303+
true | false | false
304+
project1 | project1 | true
305+
project2 | project2 | true
306+
project 1 | project2 | false
307+
end
308+
309+
with_them do
310+
it { expect(a == b).to eq(result) }
311+
312+
it 'is isomorphic' do
313+
expect(b == a).to eq(result)
314+
end
315+
end
316+
end
317+
```
318+
282319
### Matchers
283320

284321
Custom matchers should be created to clarify the intent and/or hide the

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'rspec/rails'
99
require 'shoulda/matchers'
1010
require 'rspec/retry'
11+
require 'rspec-parameterized'
1112

1213
rspec_profiling_is_configured =
1314
ENV['RSPEC_PROFILING_POSTGRES_URL'].present? ||

0 commit comments

Comments
 (0)