Skip to content

Instantly share code, notes, and snippets.

@showaltb
Last active May 4, 2016 16:09
Show Gist options
  • Save showaltb/f2ec5ddd3b9f1d353d9aca99a2889dd9 to your computer and use it in GitHub Desktop.
Save showaltb/f2ec5ddd3b9f1d353d9aca99a2889dd9 to your computer and use it in GitHub Desktop.
Example of refactoring a spec
# BEFORE
describe '#remove_existing_ife_documents' do
it 'removes any existing documents on the item with the ife_id field set' do
item.documents << FactoryGirl.create(:document, document_type: document_type)
item.documents << FactoryGirl.create(:document, document_type: document_type, ife_id: '1234')
expect(item.documents.count).to eql(2)
ife_document_creator.send(:remove_existing_ife_documents)
expect(item.documents.count).to eql(1)
end
it 'does not remove documents without the ife_id field' do
item.documents << FactoryGirl.create(:document, document_type: document_type)
item.documents << FactoryGirl.create(:document, document_type: document_type)
expect(item.documents.count).to eql(2)
ife_document_creator.send(:remove_existing_ife_documents)
expect(item.documents.count).to eql(2)
end
end
# AFTER
describe '#remove_existing_ife_documents' do
before(each) do
item.documents << FactoryGirl.create(:document, document_type: document_type)
item.documents << FactoryGirl.create(:document, document_type: document_type, ife_id: ife_id)
end
let(:remove_existing) { ife_document_creator.send(:remove_existing_ife_documents) }
let(:num_documents) } { item.documents.count }
describe 'with an ife_id' do
let(:ife_id) { '1234' }
it 'removes the document' do
expect { remove_existing }.to change { num_documents }.from(2).to(1)
end
end
describe 'without an ife_id' do
let(:ife_id) { nil }
it 'does not remove the document' do
expect { remove_existing }.not_to change { num_documents }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment