Skip to content

Instantly share code, notes, and snippets.

@mbj
Created December 19, 2012 19:53
Show Gist options
  • Save mbj/4339898 to your computer and use it in GitHub Desktop.
Save mbj/4339898 to your computer and use it in GitHub Desktop.
RFC: Uncovered mutation example for blog post by @solnic
class Book
attr_reader :sections
def initialize
@sections = []
@index = {}
end
def section(number)
@index.fetch(number) do
raise "Book does not have a section with number: #{number}"
end
end
def <<(section)
@sections << section
@index[section.number] = section # Statement deletion will delete this statement and side effects are uncovered from append_spec.rb
self
end
end
require 'spec_helper'
describe Book, '#<<' do
let(:object) { Book.new }
let(:section) { Section.new(1) }
subject { book << section }
it 'should return self' do
should be(object)
end
it 'should add section to book' do
expect { subject }.to change { object.sections.include?(section) }.from(false).to(true)
end
end
require 'spec_helper'
describe Book, '#section' do
let(:object) { described_class.new }
let(:section) { Section.new(1) }
subject { object.section(number) }
before do
object << section
end
context 'when section exists' do
let(:number) { 1 }
it 'should return section' do
should be(section)
end
end
context 'when section does not exist' do
let(:number) { 2 }
it 'should raise error' do
expect { subject }.to raise_error(RuntimeError, "Book does not have section with number: 2")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment