Skip to content

Instantly share code, notes, and snippets.

@stuliston
Last active December 27, 2015 03:28
Show Gist options
  • Save stuliston/7259215 to your computer and use it in GitHub Desktop.
Save stuliston/7259215 to your computer and use it in GitHub Desktop.
A or B, with reasons
# 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
# 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
@ashmckenzie
Copy link

I prefer B for the following reasons:

  • DRY
  • it block is lean and very easy to understand as it's an assertion only (no setup)
  • Use of let's which is generally encouraged (http://betterspecs.org/)

@stuliston
Copy link
Author

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%)

@Freaky-namuH
Copy link

B ditto @ashmckenzie. The expectation is much clearer

@compactcode
Copy link

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.

@stuliston
Copy link
Author

cheers for having a look, @compactcode. Although I'm on the fence, I share that exact problem

@stuliston
Copy link
Author

@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