timhwang21.gitbook.io
  • Intro
  • Blog
    • 2023
    • 2022
    • 2021
    • 2020
    • 2019
  • CLI
    • Git
      • Bulk resolve merge conflicts
      • git rebase-onto
      • Patch from diff
    • Shell
      • Recipes
    • Vim
      • Batch editing
      • Buffers
      • <Ctrl-r>
      • External commands
      • :global
      • Help
      • Registers
      • Splits
    • Tools
  • Programming
    • React
      • Testing Styled Components
    • Typescript
      • curryRecord
      • Exhaustive conditionals with ADTs
      • newtype
      • OmitTypes
      • Safe JSON clone
      • Type inferrers
      • XOR type
    • Ruby
      • Pry
      • RSpec
    • Rails
      • Attributes
      • Cheap many to many
      • (Don't use) counter cache
    • Databases
      • Metrics
      • Testing indexes
  • Personal
    • Uses
    • Github
    • Medium
    • LinkedIn
    • Photography
Powered by GitBook
On this page
  • Subject should match what is being described
  • .to match vs. .to eq
  • Diffing JSON blobs
  1. Programming
  2. Ruby

RSpec

Certain RSpec patterns.

Subject should match what is being described

Instead of:

spec.rb
subject { instance }

before do
  instance.update!(something: something)
end

expect(subject).to ...

It is clearer to handle the update in subject and then frame the assertion as a change.

subject { instance.update!(something: something) }

expect { subject }.to change { ... }

.to match vs. .to eq

Unlike in Jest, subset matchers only work with the looser .to match. When using Jest, expect.objectContaining, etc. can be used with both .toEqual and .toBe. However, a_hash_including, etc. will simply fail when used with .to eq! This is especially frustrating because nothing about the error message indicates that the failure comes from using the wrong matcher.

Diffing JSON blobs

PreviousPryNextRails

Last updated 3 years ago

RSpec inlines JSON blobs by default, which makes diffs very hard to detect. The gem helps with this by printing the diff line-by-line.

super_diff