Skip to content

Instantly share code, notes, and snippets.

@intinig
Forked from ryanb/quiz.rb
Created May 15, 2009 16:59
Show Gist options
  • Save intinig/112300 to your computer and use it in GitHub Desktop.
Save intinig/112300 to your computer and use it in GitHub Desktop.
# COMMUNITY CHALLENGE
#
# How would you test this Quiz#problem method? No mocks/stubs allowed.
class Quiz
def initialize(input = STDIN, output = STDOUT)
@input = input
@output = output
end
def problem
first = rand(10)
second = rand(10)
@output.puts "What is #{first} + #{second}?"
answer = @input.gets
if answer.to_i == first + second
@output.puts "Correct!"
else
@output.puts "Incorrect!"
end
end
end
require "rubygems"
require 'spec/autorun'
describe "Quiz" do
before do
@io = mock("IO")
@quiz = Quiz.new(@io, @io)
end
it "should work with a correct answer" do
@quiz.should_receive(:rand).ordered.twice.and_return(7, 6)
@io.should_receive(:puts).ordered.with("What is 7 + 6?")
@io.should_receive(:gets).ordered.and_return("13")
@io.should_receive(:puts).ordered.with("Correct!")
@quiz.problem
end
it "should work with an incorrect answer" do
@quiz.should_receive(:rand).ordered.twice.and_return(5, 9)
@io.should_receive(:puts).ordered.with("What is 5 + 9?")
@io.should_receive(:gets).ordered.and_return("foobar")
@io.should_receive(:puts).ordered.with("Incorrect!")
@quiz.problem
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment