Last active
June 16, 2017 15:24
-
-
Save kellysutton/337b12d1f4a0aaa4c2d7e6b8926ee3cf to your computer and use it in GitHub Desktop.
Count the Contexts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe User do | |
describe '#full_name' do | |
subject do | |
described_class.new(first_name, middle_name, last_name) | |
end | |
let(:first_name) { 'Roy' } | |
let(:last_name) { 'Biv' } | |
context 'middle name is given' do | |
let(:middle_name) { 'Gee' } | |
it { is_expected.to eq('Roy G. Biv') } | |
end | |
context 'middle name is not given' do | |
let(:middle_name) { nil } | |
it { is_expected.to eq('Roy Biv') } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FrequentFlier | |
def build(customer, starting_balance = nil, status_level = nil) | |
starting_balance = promotion_starting_balance || starting_balance || 0 | |
status_level = promotion_starting_status || status_level | |
# Family members of airline employees get so many perks! | |
if customer.related_to_employee? | |
starting_balance += 10_000 | |
if ![:diamond, :platinum].include?(status_level) | |
status_level = :gold | |
end | |
end | |
{ | |
account_number: AccountNumber.generate, | |
balance: starting_balance || 0, | |
status_level: status_level || nil, | |
miles_multiplier: customer&.miles_multiplier || 1 | |
} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe FrequentFlier do | |
subject do | |
described_class.build(customer, starting_balance, status_level) | |
end | |
let(:customer) do | |
build(:customer, related_to_employee: related_to_employee) | |
end | |
let(:starting_balance) { 1_000 } | |
let(:status_level) { nil } | |
context 'customer is related to an employee' do | |
let(:related_to_employee) { true } | |
it do | |
is_expected.to eq({ | |
account_number: an_instance_of(AccountNumber), | |
balance: 11_000, | |
status_level: :gold, | |
miles_multiplier: 1 | |
}) | |
end | |
end | |
context 'customer is not related to an employee' do | |
let(:related_to_employee) { false } | |
it do | |
is_expected.to eq({ | |
account_number: an_instance_of(AccountNumber), | |
balance: 1_000, | |
status_level: nil, | |
miles_multiplier: 1 | |
}) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestUser | |
def test_full_name_includes_the_middle_initial_when_middle_name_is_given | |
user = User.new('Roy', 'Gee', 'Biv') | |
assert user.full_name == 'Roy G. Biv' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestUser | |
def test_full_name_includes_the_middle_initial_when_middle_name_is_given | |
user = User.new('Roy', 'Gee', 'Biv') | |
assert user.full_name == 'Roy G. Biv' | |
end | |
def test_full_name_does_not_include_middle_initial_when_no_middle_name_is_given | |
user = User.new('Roy', nil, 'Biv') | |
assert user.full_name == 'Roy Biv' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe User do | |
describe '#full_name' do | |
subject do | |
described_class.new(first_name, middle_name, last_name) | |
end | |
let(:first_name) { 'Roy' } | |
let(:last_name) { 'Biv' } | |
context 'middle name is given' do | |
let(:middle_name) { 'Gee' } | |
it { is_expected.to eq('Roy G. Biv') } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment