Last Updated: February 25, 2016
·
1.469K
· ejholmes

Use lambdas for RSpec subjects

You can pass a lambda to RSpec's subject block for more concise expectations:

describe Car do
  describe '.accelerate' do
    subject { -> { car.accelerate } }

    it { should change(car, :speed).by(1) }
  end

  describe '.boomtown' do
    subject { -> { car.boomtown } }

    it { should raise_error }
  end
end