Skip to content

Instantly share code, notes, and snippets.

@leandro
Last active November 20, 2018 02:12
Show Gist options
  • Save leandro/091dd5814afc5512957bd3a4c01f6f3f to your computer and use it in GitHub Desktop.
Save leandro/091dd5814afc5512957bd3a4c01f6f3f to your computer and use it in GitHub Desktop.
Practical (or too dirty?) way of testing something
require 'spec_helper'
shared_examples_for 'durable_record' do
let(:duration) { nil }
let(:end_date) { nil }
let(:start_date) { nil }
let(:instance) { described_class.new }
let(:attributes) do
{ duration: duration, end_date: end_date, start_date: start_date }
end
subject do
pure_assign(instance, attributes)
instance.send(method, value)
instance.attributes.values_at(*%w[duration end_date start_date])
end
describe '#duration=' do
let(:method) { :duration= }
context 'when start_date is set and duration provided is not nil' do
let(:start_date) { Date.civil(2018, 11, 19) }
let(:value) { 2 }
let(:expected_end_date) { Date.civil(2018, 11, 21) }
it { is_expected.to eq([value, expected_end_date, start_date]) }
end
context 'when end_date is set and duration provided is not nil' do
let(:end_date) { Date.civil(2018, 11, 19) }
let(:value) { 4 }
let(:expected_start_date) { Date.civil(2018, 11, 15) }
it { is_expected.to eq([value, end_date, expected_start_date]) }
end
context 'when start_date and end_date are set and duration provided is nil' do
let(:start_date) { Date.civil(2018, 11, 19) }
let(:end_date) { Date.civil(2018, 11, 22) }
let(:value) { nil }
let(:expected_duration) { 3 }
it { is_expected.to eq([expected_duration, end_date, start_date]) }
end
context 'when all three attributes are not set and duration is provided' do
let(:value) { 3 }
it { is_expected.to eq([value, nil, nil]) }
end
end
describe '#end_date=' do
let(:method) { :end_date= }
context 'when start_date is set and end_date provided is not nil' do
let(:start_date) { Date.civil(2018, 11, 14) }
let(:value) { Date.civil(2018, 11, 21) }
let(:expected_duration) { 7 }
it { is_expected.to eq([expected_duration, value, start_date]) }
end
context 'when duration is set and end_date provided is not nil' do
let(:duration) { 2 }
let(:value) { Date.civil(2018, 11, 20) }
let(:expected_start_date) { Date.civil(2018, 11, 18) }
it { is_expected.to eq([duration, value, expected_start_date]) }
end
context 'when duration and start_date are set and end_date provided is nil' do
let(:duration) { 3 }
let(:start_date) { Date.civil(2018, 11, 12) }
let(:value) { nil }
let(:expected_end_date) { Date.civil(2018, 11, 15) }
it { is_expected.to eq([duration, expected_end_date, start_date]) }
end
context 'when all three attributes are not set and end_date is provided' do
let(:value) { Date.civil(2018, 11, 17) }
it { is_expected.to eq([nil, value, nil]) }
end
end
describe '#start_date=' do
let(:method) { :start_date= }
context 'when duration is set and start_date provided is not nil' do
let(:duration) { 10 }
let(:value) { Date.civil(2018, 11, 15) }
let(:expected_end_date) { Date.civil(2018, 11, 25) }
it { is_expected.to eq([duration, expected_end_date, value]) }
end
context 'when end_date is set and start_date provided is not nil' do
let(:end_date) { Date.civil(2018, 11, 20) }
let(:value) { Date.civil(2018, 11, 15) }
let(:expected_duration) { 5 }
it { is_expected.to eq([expected_duration, end_date, value]) }
end
context 'when duration and end_date are set and start_date provided is nil' do
let(:duration) { 3 }
let(:end_date) { Date.civil(2018, 11, 12) }
let(:value) { nil }
let(:expected_start_date) { Date.civil(2018, 11, 9) }
it { is_expected.to eq([duration, end_date, expected_start_date]) }
end
context 'when all three attributes are not set and end_date is provided' do
let(:value) { Date.civil(2018, 11, 19) }
it { is_expected.to eq([nil, nil, value]) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment