Skip to content

Instantly share code, notes, and snippets.

@rishav
Forked from intinig/quiz.rb
Created May 15, 2009 19:23
Show Gist options
  • Save rishav/112393 to your computer and use it in GitHub Desktop.
Save rishav/112393 to your computer and use it in GitHub Desktop.
# COMMUNITY CHALLENGE
#
# A little modification to @intinig solution ( to test the order as well )
# May not be the best way to solve the problem.
#
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 "test/unit"
require "rubygems"
require "mocha"
class QuizTest < Test::Unit::TestCase
class SimpleIO
attr_reader :buffer
def initialize
@buffer = ""
end
def puts(s)
@buffer << s
end
def gets
@buffer << "13"
"13"
end
end
def setup
@io = SimpleIO.new
@quiz = Quiz.new(@io, @io)
end
def test_correct
@quiz.expects(:rand).times(2).returns(7, 6)
@quiz.problem
assert_equal "What is 7 + 6?13Correct!", @io.buffer
end
def test_incorrect
@quiz.expects(:rand).times(2).returns(5, 9)
@quiz.problem
assert_equal "What is 5 + 9?13Incorrect!", @io.buffer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment