-
-
Save stuliston/7259215 to your computer and use it in GitHub Desktop.
A or B, with reasons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A. Less nested contexts, clearer object collaboration at the cost of repetition | |
describe PlaceParameters do | |
describe '#sort_by' do | |
it 'concatenates the sort_by and sort_dir params' do | |
params = { sort_by: 'name', sort_dir: 'asc' } | |
sort_by = PlaceParameters.new(params).sort_by | |
expect(sort_by).to match /^name asc$/i | |
end | |
it 'explicitly defaults sort direction to ASC' do | |
params = { sort_by: 'name' } | |
sort_by = PlaceParameters.new(params).sort_by | |
expect(sort_by).to match /^name asc$/i | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# B. Contexts used to DRY assertions, object collaboration less obvious at a glance | |
describe PlaceParameters do | |
let(:place_parameters) { PlaceParameters.new(params) } | |
describe '#sort_by' do | |
context 'when sort_by and sort_dir are specified' do | |
let(:params) { {sort_by: 'name', sort_dir: 'asc'} } | |
it 'concatenates them' do | |
expect(place_parameters.sort_by).to match /^name asc$/i | |
end | |
end | |
context 'when sort_dir is not specified' do | |
let(:params) { {sort_by: 'name'} } | |
it 'explicitly defaults sort direction to ASC' do | |
expect(place_parameters.sort_by).to match /^name asc$/i | |
end | |
end | |
end | |
end |
To play devil's advocate:
- A makes the collaboration of params and PlaceParams much more obvious, more accurately reflecting how you'd use the two objects in real implementation code.
- A is, with the same attention to clarity in both the test layout and the resulting output, far less lines (30%)
B ditto @ashmckenzie. The expectation is much clearer
A. I value tests that are easy to read and change over DRY.
With B, I have to jump to two different lines outside of the test to work out what is going on.
cheers for having a look, @compactcode. Although I'm on the fence, I share that exact problem
@gabrielrotbart suggested this.
It still uses the contexts to set up each example differently, but repeats the interaction between the PlaceParameter and the params object for clarity - mirroring implementation code:
describe PlaceParameters do
describe '#sort_by' do
context 'when sort_by and sort_dir are specified' do
let(:params) { {sort_by: 'name', sort_dir: 'asc'} }
it 'concatenates them' do
sort_by = PlaceParameters.new(params).sort_by
expect(sort_by).to match /^name asc$/i
end
end
context 'when sort_dir is not specified' do
let(:params) { {sort_by: 'name'} }
it 'explicitly defaults sort direction to ASC' do
sort_by = PlaceParameters.new(params).sort_by
expect(sort_by).to match /^name asc$/i
end
end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I prefer B for the following reasons: