モックを使って外部リソースを使うソフトをテストしよう

スポンサーリンク

とっても素敵なモックライブラリflexmock

class Questioner
def initialize(input = STDIN, output = STDOUT)
@input = input
@output = output
end
def ask(question)
@output.puts question
response = @input.gets.chomp
case(response)
when /^y(es)?$/i
true
when /^no?$/i
false
else
@output.puts "I don't understand."
ask question
end
end
end

require "test/unit"
require_relative "test_unit_extensions"
require_relative "questioner_with_string_io"
require "flexmock/test_unit"
class QuestionerTest < Test::Unit::TestCase
def setup
@input = flexmock("input")
@output = flexmock("output")
@questioner = Questioner.new(@input, @output)
@question = "Are you happy?"
end
%w[y Y YeS YES yes].each do |y|
must "return true when parsing #{y}" do
expect_output @question
provide_input(y)
assert @questioner.ask(@question), "Expected #{y} to be true"
end
end
%w[n N no nO].each do |no|
must "return false when parsing #{no}" do
expect_output @question
provide_input(no)
assert !@questioner.ask(@question), "Expected #{no} to be false"
end
end
[["y", true], ["n", false]].each do |input, state|
must "continue to ask for input until given #{input}" do
%w[Yesterday North kittens].each do |i|
expect_output @question
provide_input(i)
expect_output("I don't understand.")
end
expect_output @question
provide_input(input)
assert_equal state, @questioner.ask(@question)
end
end
def provide_input(string)
@input.should_receive(:gets => string).once
end
def expect_output(string)
@output.should_receive(:puts).with(string).once
end
end

コメント

タイトルとURLをコピーしました