-
-
Save intinig/112300 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
# 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