Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 2.2 KB

File metadata and controls

94 lines (73 loc) · 2.2 KB

RSpec

In general, we try to adhere to the BetterSpecs guidelines. This document is a few extensions and key points from that.

Key points

Syntax

  • let blocks should use multiple line { } proc syntax. { } syntax is less distracting than do end [link]

    # Bad
    let(:foo) do
      some_very_long_instantiation
    end
    
    # Good
    let(:bar) {
      some_very_long_instantiation
    }
    
    # Also good
    let(:baz) { 'baz' }
  • Use { } blocks for expectations [link]

    # bad
    expect do
      something
    end.to change(Something, :else)
    
    # good
    expect { something }.to change(Something, :else)
    
    # also good
    expect {
      something
    }.to change(Something, :else)
  • Use do end for before blocks [link]

    # bad
    before {
      do_something
    }
    
    # ok
    before { do_something }
    
    # good
    before do
      do_something
    end

    The difference between before and let/expect is due to how it reads. e.g let(:foo) { :bar } reads like 'let foo be equal to bar' whereas before do something; end reads like 'before the test, do this thing'.

  • Extract hardcoded values to well-named let blocks, in the same manner as constants [link]

    # bad
    expect(1 + 2).to eq(3)
    
    # good
    subject { 1 + 2 }
    let(:expected_result) { 3 }
    
    it { is_expected.to eq(expected_result)}