-
-
Save joshknowles/112335 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 | |
# | |
# How would you test this Quiz#problem method? Only two rules: | |
# | |
# 1. No mocks or stubs allowed. I'm looking for a other alternatives. | |
# Mocks can get messy in complex scenarios, and this is intended to | |
# be a high level test which executes all code. I don't think mocking | |
# would be a very clean solution anyway, but if you want to try it | |
# and prove me wrong feel free. | |
# | |
# 2. No changing the Quiz class implementation/structure. But you can | |
# use whatever framework you want for the tests. | |
# | |
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" | |
require "spec/autorun" | |
describe Quiz do | |
before :each do | |
@input = mock(:input) | |
@output = mock(:output) | |
@quiz = Quiz.new(@input, @output) | |
@quiz.stub!(:rand).and_return(1, 2) | |
end | |
it "should output 'Correct!' if the input is the sum of the two numbers given in the question" do | |
@output.should_receive(:puts).ordered.with("What is 1 + 2?") | |
@input.should_receive(:gets).ordered.and_return(3) | |
@output.should_receive(:puts).ordered.with("Correct!") | |
@quiz.problem | |
end | |
it "should output 'Incorrect!' if the input is not the sum of the two numbers given in the question" do | |
@output.should_receive(:puts).ordered.with("What is 1 + 2?") | |
@input.should_receive(:gets).ordered.and_return(4) | |
@output.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