如何使用 RSpec 测试 STDIN [英] how to test STDIN with RSpec
问题描述
好的,需要帮助进行测试.我想测试这个类是否收到一个字母O"和当调用move_computer"方法时,返回该人在 cli 上输入的任何内容.我的心理子处理器告诉我,这是一个简单的将变量分配给某个东西以保存 STDIN 中的随机人工输入的方法.只是现在没有得到它......有人指出我正确的方向吗?
Ok, need help working out a test. I want to test that this class receives a letter "O" and that when called the "move_computer" method returns WHATEVER the person enters on the cli. my mental subprocessor tells me this is a simple assign a variable to something to hold the random human input at STDIN. Just not getting it right now...anyone point me in the right direction?
这是我的课...
class Player
def move_computer(leter)
puts "computer move"
@move = gets.chomp
return @move
end
end
我的测试看起来像...
my test look like...
describe "tic tac toe game" do
context "the player class" do
it "must have a computer player O" do
player = Player.new()
player.stub!(:gets) {"\n"} #FIXME - what should this be?
STDOUT.should_receive(:puts).with("computer move")
STDOUT.should_receive(:puts).with("\n") #FIXME - what should this be?
player.move_computer("O")
end
end
end
推荐答案
因为 move_computer
returns 输入,我想你的意思是:
Because move_computer
returns the input, I think you meant to say:
player.move_computer("O").should == "\n"
我会这样写完整的规范:
I would write the full spec like this:
describe Player do
describe "#move_computer" do
it "returns a line from stdin" do
subject.stub!(:gets) {"penguin banana limousine"}
STDOUT.should_receive(:puts).with("computer move")
subject.move_computer("O").should == "penguin banana limousine"
end
end
end
这篇关于如何使用 RSpec 测试 STDIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!