Skip to content

Instantly share code, notes, and snippets.

@zealoushacker
Created November 5, 2014 23:27
Show Gist options
  • Save zealoushacker/83833a4f8eab9dada934 to your computer and use it in GitHub Desktop.
Save zealoushacker/83833a4f8eab9dada934 to your computer and use it in GitHub Desktop.
Person object with specs
class Person
attr_reader :first_name, :last_name, :gender
def self.genders
[:male, :female]
end
def initialize first_name, last_name, gender
unless Person.genders.include? gender
raise "Can't birth people, unless they are #{Person.genders}"
end
@first_name = first_name
@last_name = last_name
@gender = gender
end
end
require_relative '../person'
require 'securerandom'
describe Person do
it "can have genders" do
expect(Person.genders).to eq([:male, :female])
end
describe "Birthin'" do
before :each do
@first_or_last_name = SecureRandom.hex(5)
@gender = :male
@person = Person.new(
@first_or_last_name,
@first_or_last_name,
@gender
)
end
it "can be birthed with a first name" do
expect(@person.first_name).to eq(@first_or_last_name)
end
it "can be birthed with a last name" do
expect(@person.last_name).to eq(@first_or_last_name)
end
it "can be birthed with a gender" do
expect(@person.gender).to eq(@gender)
end
describe "validation" do
it "should not be birthable unless it's one of the genders" do
expect { Person.new("", "", :horse) }.to(
raise_error(
"Can't birth people, unless they are #{Person.genders}"
)
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment