Skip to content

Instantly share code, notes, and snippets.

@ihower
Created November 12, 2012 08:03
Show Gist options
  • Save ihower/4058073 to your computer and use it in GitHub Desktop.
Save ihower/4058073 to your computer and use it in GitHub Desktop.
# Use constant directly
class V1
def caculate
Math::PI
end
end
# Use a method
class V2
def pi
Math::PI
end
def caculate
pi
end
end
# Use dependency injection
class V3
def initialize(pi=Math::PI)
@pi = pi
end
def caculate
@pi
end
end
describe V1 do
it "should return PI" do
my_custom_pi = 3.14
stub_const("Math::PI", my_custom_pi) # RSpec 2.11 new feature. Very magic!
V1.new.caculate.should == my_custom_pi
end
end
describe V2 do
it "should return PI" do
my_custom_pi = 3.14
v2 = V2.new
v2.stub(:pi).and_return(my_custom_pi) # stub the method
v2.caculate.should == my_custom_pi
end
end
describe V3 do
it "should return PI" do
my_custom_pi = 3.14
v3 = V3.new(my_custom_pi) # inject the dependency. Need not any magic stub.
v3.caculate.should == my_custom_pi
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment