diff options
author | Nick Thomas <nick@gitlab.com> | 2017-08-10 18:23:56 +0100 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2017-08-11 12:57:06 +0100 |
commit | caa498fd315fde3d4189da0b0293961dbcfa5e92 (patch) | |
tree | 1d68d434b324691de91ef900219853c4c0a4c4b1 /doc/development | |
parent | b1ae717de64eee22266be4c46807009b16c64ba9 (diff) | |
download | gitlab-ce-caa498fd315fde3d4189da0b0293961dbcfa5e92.tar.gz |
Use rspec-parameterized for table-based tests
Diffstat (limited to 'doc/development')
-rw-r--r-- | doc/development/testing.md | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/doc/development/testing.md b/doc/development/testing.md index 3d5aa3d45e9..56dc8abd38a 100644 --- a/doc/development/testing.md +++ b/doc/development/testing.md @@ -268,6 +268,43 @@ end - Avoid scenario titles that add no information, such as "successfully". - Avoid scenario titles that repeat the feature title. +### Table-based / Parameterized tests + +This style of testing is used to exercise one piece of code with a comprehensive +range of inputs. By specifying the test case once, alongside a table of inputs +and the expected output for each, your tests can be made easier to read and more +compact. + +We use the [rspec-parameterized](https://github.com/tomykaira/rspec-parameterized) +gem. A short example, using the table syntax and checking Ruby equality for a +range of inputs, might look like this: + +```ruby +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 + it { expect(a == b).to eq(result) } + + it 'is isomorphic' do + expect(b == a).to eq(result) + end + end +end +``` + ### Matchers Custom matchers should be created to clarify the intent and/or hide the |