Last active
August 29, 2015 14:01
-
-
Save themoxman/104b1e4a4e55d4403e91 to your computer and use it in GitHub Desktop.
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 User | |
# justin rarely mutates initially set instance variables | |
attr_reader :first_name, :last_name, :birth_date | |
def initialize(first_name, last_name, birth_date=nil) | |
@first_name = first_name | |
@last_name = last_name | |
@birth_date = birth_date | |
end | |
def full_name | |
"#{first_name} #{last_name}" | |
end | |
def age | |
bday = Date.parse(birth_date) | |
today = Date.today | |
((today - bday)/365).to_i | |
end | |
end | |
# note, a big take away is that we're not stubbing the current object. | |
# stub externals, in this case the Date class | |
describe User do | |
let(:today) { Date.parse("2014-05-08") } | |
let(:user) { User.new("Dave", "Mox", "05/05/1989") } | |
before do | |
# Date.stub(:today).and_return(date) | |
Date.stub(today: today) # equivalent to above | |
end | |
it "has an age" do | |
expect(user.age).to eq(25) | |
end | |
context "last year" do | |
let(:today) { Date.parse("2013-05-08") } | |
it "was one year younger" do | |
# Date.stub(:today).and_return(date) | |
expect(user.age).to eq(24) | |
end | |
end | |
end | |
# with Objects | |
describe User do | |
it "has a first_name" do | |
user = User.new("Dave", "Mox") | |
expect(user.first_name).to eq("Dave") | |
end | |
it "has a last name" do | |
user = User.new("Dave", "Mox") | |
expect(user.last_name).to eq("Mox") | |
end | |
it "has a full name" do | |
user = User.new("Dave", "Mox") | |
expect(user.full_name).to eq("Dave Mox") | |
end | |
end | |
# with Test Doubles | |
# A test double is an object that stands in for another object in your system | |
# during a code example. Use the double method, passing in an optional | |
# identifier, to create one. | |
describe User do | |
it "has a first_name" do | |
# newest version of RSpec makes #instance_double available. | |
user = double("User", first_name: "Dave") | |
expect(user.first_name). to eq("Dave") | |
end | |
it "has a last_name" do | |
user = double("User", last_name: "Mox") | |
expect(user.last_name). to eq("Mox") | |
end | |
end | |
# with Method Stubs | |
# A method stub is an implementation that returns a pre-determined value. Method | |
# stubs can be declared on test doubles or real objects using the same syntax. | |
describe User do | |
it "has a full_name" do | |
user = double("User") | |
user.stub(:full_name).and_return("Dave Mox") | |
expect(user.full_name).to eq("Dave Mox") | |
end | |
end | |
# create test double and declare method stub in one statement | |
describe User do | |
it "has a full_name" do | |
user = double("User", full_name: "Dave Mox") | |
expect(user.full_name).to eq("Dave Mox") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment