-
-
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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: