RSpec:使用方法存根测试交互式用户输入 [英] RSpec: Using a method stub to test interactive user input
问题描述
我是 Ruby 和 RSpec 的新手,并且已经花了几个小时尝试编写测试交互式 井字棋程序,但没有找到任何有用的答案来解决我遇到的错误:
I'm new to both Ruby and RSpec, and have already spent hours trying to write the first steps for testing an interactive tic tac toe program, but haven't located any useful answers to the error I'm getting:
> bundle exec rspec --format documentation
Do you want to play tic-tac-toe? (y/n)
An error occurred while loading ./spec/tic_tac_toe_spec.rb.
Failure/Error: choice = gets.chomp.downcase
Errno::ENOENT:
No such file or directory @ rb_sysopen - --format
# ./lib/tic_tac_toe.rb:70:in `gets'
# ./lib/tic_tac_toe.rb:70:in `gets'
# ./lib/tic_tac_toe.rb:70:in `start_game'
# ./lib/tic_tac_toe.rb:144:in `<top (required)>'
# ./spec/tic_tac_toe_spec.rb:1:in `require'
# ./spec/tic_tac_toe_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.0004 seconds (files took 0.08415 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
当您开始游戏时,它会提示您是否要玩(y/n).我已经尝试过使用模拟和存根的不同变体,但总是得到同样的错误.我想我错过了一些非常基本但看不到的东西.
When you start the game, it prompts whether you want to play (y/n). I've already attempted different variations of using mocking and stubs but always get this same error. I imagine I'm missing something very basic but cannot see it.
问题:如何充分解决(使用 RSpec)游戏的第一个提示?
Question: how would one adequately address (using RSpec) this first prompt of the game?
游戏使用一个独立的方法(start_game
)而不是一个类来启动.我正在寻找某种方式使用一些默认输入(如 y
或 n
)来模拟或存根,因为我希望测试是自动化的.但是,我看到的所有示例都使用类来启动程序(而不是像这里这样的独立方法).
The game uses an independent method (start_game
) but not a class to start. I'm looking for some way mock or stub this with some default input (like y
or n
) as I want the tests to be automated. All the examples I see use a class to start a program, however (rather than a stand-alone method like here).
目前在 spec/tic_tac_toe_spec.rb
中有一些 let
关键字用于 :output
和 :game
我从某个地方的一个例子中发现,但显然这不起作用.
Currently in spec/tic_tac_toe_spec.rb
there are some let
keywords for :output
and :game
which I found from an example somewhere but obviously this doesn't work.
编辑 我想测试以下方法.RSpec 代码在 choice = gets.chomp.downcase
行上一直卡住.
Edit I want to test the following method. The RSpec code keeps choking on the choice = gets.chomp.downcase
line.
def start_game
puts "Do you want to play tic-tac-toe? (y/n)"
choice = gets.chomp.downcase
unless ["y","n"].include?(choice)
abort("Please answer with y or n.")
end
case choice
when "y"
puts "Ok, let's start!"
b = Board.new
puts "Enter name of player 1 (O)"
player1name = gets.chomp
player1 = Player.new(player1name)
puts "Now enter name of player 2 (X)"
player2name = gets.chomp
player2 = Player.new(player2name)
play_game(player1, player2, b)
when "n"
puts "Your loss."
end
end #start_game
推荐答案
您收到错误是因为您将 --format 文档
作为参数传递给 RSpec.
You are receiving the error because you are passing the --format documentation
as a parameter to RSpec.
gets
读取已传递的 ARGV.这也包括 RSpec 参数.您应该使用 STDIN.gets
以便您只读取标准输入而不是参数.
gets
reads the ARGV that have been passed. This also includes the RSpec parameters. You should use STDIN.gets
so that you only read standard input and not the parameters.
您可以在此问题中阅读更多相关信息:https://stackoverflow.com/a/19799223/6156030
You can read more about in this question: https://stackoverflow.com/a/19799223/6156030
这篇关于RSpec:使用方法存根测试交互式用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!