【发布时间】:2015-11-23 13:13:49
【问题描述】:
我刚刚开始一个小项目来模拟嘉年华的售票亭,其中一个准则是测试用户是否可以输入门票数量。该程序在控制台中运行,由于@Stefan 在this question 上的回答,我最终(希望)弄清楚了如何实现此测试。
问题是现在,当我运行测试文件时,minitest 说:
0 次运行,0 次断言,0 次失败,0 次错误,0 次跳过
当我尝试使用ruby path/to/test/file.rb --name method-name 按名称运行测试时,我得到了相同的结果。我不确定这是否是因为我的代码仍然有问题,或者是因为我错误地设置了 minitest。我试图在 SO 上查找类似的问题,但大多数问题似乎都涉及使用带有 rails 的 minitest,而我只有一个普通的 ruby 项目。
这是我的测试文件:
gem 'minitest', '>= 5.0.0'
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'carnival'
class CarnivalTest < MiniTest::Test
def sample
assert_equal(1, 1)
end
def user_can_enter_number_of_tickets
with_stdin do |user|
user.puts "2"
assert_equal(Carnival.new.get_value, "2")
end
end
def with_stdin
stdin = $stdin # global var to remember $stdin
$stdin, write = IO.pipe # assign 'read end' of pipe to $stdin
yield write # pass 'write end' to block
ensure
write.close # close pipe
$stdin = stdin # restore $stdin
end
end
在一个名为 carnival.rb 的文件中,与我的测试文件位于同一文件夹中
Class Carnival
def get_value
gets.chomp
end
end
如果有人能帮助找出测试未运行的原因,我将不胜感激!
【问题讨论】: