Make RSpec shared examples easier to understand with this one trick!
09 Jul 15:51

Look at this:

let!(:variable_1) { ... }
let(:variable_b) { ... }
let!(:varialbe_b) { ... }
let(:variable_with_a_very_long_name) { ... }
let!(:another_variable) do
  # ...
end

it_behaves_like 'does something'

Can you guess which variables are needed for the example expectations, which are only needed for setup and could be shared, and which are no longer needed after a change?

Guess not.

Now look at this:

before do
  # create something
  # create a list of somethings
end

it_behaves_like 'does something',
                score: 50,
                something_else: -> { create :something_else }

Make your tests beautiful and easy to read, and you might even begin to like writing them.

Edit

The last example needs a followup:

shared_example 'does something' do |score:, something_else:|
  it 'does something else' do
    expect(something_else.call).to eq(score)
  end
end

Comments

Empty! You must sign in to add comments.