Skip to content

Instantly share code, notes, and snippets.

@woarewe
Created February 4, 2022 11:43
Show Gist options
  • Save woarewe/6ec636e7b49677704083639acbd3369e to your computer and use it in GitHub Desktop.
Save woarewe/6ec636e7b49677704083639acbd3369e to your computer and use it in GitHub Desktop.
require 'rails_helper'
class Person
attr_reader(
:first_name,
:last_name,
:age,
:country
)
def initialize(first_name:, last_name: nil, age:, country:)
@first_name = first_name
@last_name = last_name
@age = age
@country = country
end
def full_name
"#{first_name} #{last_name}"
end
def can_buy_alco?
country.can_buy_alco?(self)
end
end
module Countries
class USA
def can_buy_alco?(person)
person.age >= 21
end
end
class Belarus
def can_buy_alco?(person)
person.age >= 18
end
end
end
describe Countries::USA do
let(:country) { described_class.new }
let(:person) { double }
before do
allow(person).to receive(:age).and_return(age)
end
context "when person's age < 21" do
let(:age) { 18 }
it { expect(country.can_buy_alco?(person)).to be(false) }
end
context "when person's age >= 21" do
let(:age) { 30 }
it { expect(country.can_buy_alco?(person)).to be(true) }
end
end
describe Countries::Belarus do
let(:country) { described_class.new }
describe "#can_buy_alco?" do
end
end
describe Person do
let(:params) do
{
first_name: first_name, last_name: last_name, age: age, country: country
}.compact
end
let(:first_name) { "Don" }
let(:last_name) { nil }
let(:country) { Countries::USA.new }
let(:age) { 40 }
describe "#full_name" do
subject(:full_name) do
described_class.new(**params).full_name
end
let(:last_name) { nil }
let(:age) { 50 }
context "when first name and last name are present" do
let(:first_name) { "Don" }
let(:last_name) { "Keeler" }
it { expect(full_name).to eq("#{first_name} #{last_name}") }
end
context "when last name is not passed" do
let(:first_name) { "Don" }
it { expect(full_name).to eq("#{first_name} #{last_name}") }
end
end
describe '#can_buy_alco?' do
let(:person) { Person.new(**params) }
context "when person can buy alco in a country" do
let(:country) { double }
before do
allow(country).to receive(:can_buy_alco?).with(person).and_return(true)
end
it do
expect(person.can_buy_alco?).to be(true)
end
end
context "when person can not buy alco in a country" do
let(:country) { double }
before do
allow(country).to receive(:can_buy_alco?).with(person).and_return(false)
end
it do
expect(person.can_buy_alco?).to be(false)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment