Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Created September 25, 2013 08:50
Show Gist options
  • Save brandoncordell/6696858 to your computer and use it in GitHub Desktop.
Save brandoncordell/6696858 to your computer and use it in GitHub Desktop.
# Example - Let's write a function that prints a name
# This is RSpec, it's a testing framework. It's just like ruby in that it's made to be super readable. As you read the code, read it outloud like instructions
# We start with tests first
# We know that we need a function to print names. That's all we need. We
# use tests to keep us focused on the task. You'll write it piece by piece. Instead of writing the whole
# function, we'll break it up into responsibility chunks
# We know we'll need to print a name given to us, so lets write our test
describe NameSpeak do
context "with a name" do
it "should print the name" do
speaker = NameSpeak.new
response = speaker.speak('Brandon')
response.should be 'Hello, Brandon!'
end
end
end
# At this point we run our test (and it fails obviously)
# Let's write the function
class NameSpeak
def speak(name)
puts 'Hello, #{name}!'
end
end
# At this point our spec passes, but what if they don't provide a name???
# Let's amend our test
describe NameSpeak do
context "given a name" do
it "should print the name" do
speaker = NameSpeak.new
response = speaker.speak('Brandon')
response.should be 'Hello, Brandon!'
end
end
context "without a name" do
it "should say a little quip" do
speaker = NameSpeak.new
response = speaker.speak
response.should be 'Hello, Douchebag!'
end
end
end
# Again the test will fail (red), let's make it green
class NameSpeak
def speak(name)
puts 'Hello, Douchebag!' and return if name.nil?
puts 'Hello, #{name}!'
end
end
# and it passes!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment