-
-
Save rishav/112393 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
# 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