Skip to content

Instantly share code, notes, and snippets.

@armandofox
Last active August 20, 2020 18:57
Show Gist options
  • Save armandofox/7ba2377b49261a1d97c3109b99a4dbf0 to your computer and use it in GitHub Desktop.
Save armandofox/7ba2377b49261a1d97c3109b99a4dbf0 to your computer and use it in GitHub Desktop.
unit_test_summary.rb
# 1. Pure leaf function: test critical values and noncritical regions
it 'occurs when multiple of 4 but not 100' do
expect(leap?(2008)).to be_truthy
end
it 'does not occur when multiple of 400' do
expect(leap?(2000)).to be_falsy
end
# 2. Using doubles for explicit dependencies such as collaborators
# UI.background() calls Defcon.level() to determine display color
it 'colors the UI red if Defcon is 2 or lower' do
# Arrange: stub Defcon to return 2
allow(Defcon).to receive(:level).and_return(2)
expect(UI.background).to eq('red') # Act and Assert
end
# 3. Has implicit dependencies such as time
it 'runs backups on Tuesdays' do
# Arrange: stub Date.today to return Tues 2020-02-04
allow(Date).to receive(:today).and_return(Time.local(2020,2,4))
expect(run_backups_today?()).to be_truthy # Act and Assert
end
# 4. Has side effects (verbose version)
it 'lowers Defcon level by 1' do
# Arrange: check previous value of state
before = Defcon.level()
post_alert("Hostile craft detected") # Act
expect(Defcon.level()).to eq(before - 1) # Asset
end
# Shortcut version passing a callable to `expect`
it 'lowers Defcon level by 1' do
expect { post_alert("Hostile craft detected") }.
to change { Defcon.level() }.by(-1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment