gpt4 book ai didi

ruby - Minitest - 测试不运行 - 没有 Rails

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:32 26 4
gpt4 key购买 nike

我刚刚开始一个模拟嘉年华售票亭的小项目,其中一项准则是测试用户是否可以输入门票数量。该程序在控制台中运行,由于@Stefan 在 this question 上的回答,我最终(希望)弄清楚了如何实现此测试。 .

问题是现在,当我运行测试文件时,minitest 说:

0 runs, 0 assertions, 0 failures, 0 errors, 0 skips

当我尝试使用 ruby path/to/test/file.rb --name method-name 按名称运行测试时,我得到了相同的结果。我不确定这是不是因为我的代码仍然有问题,或者是因为我错误地设置了 minitest。我试图在 SO 上查找类似的问题,但大多数问题似乎都涉及将 minitest 与 rails 一起使用,而我只有一个普通的 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

如果有人能帮助找出为什么测试没有运行,我将不胜感激!

最佳答案

按照惯例,Minitest 中的测试是以test_ 开头的公共(public)实例方法,因此原始测试没有实际的测试方法。您需要更新您的测试类,以便带有断言的方法遵循以下约定:

class CarnivalTest < Minitest::Test
def test_sample
assert_equal(1, 1)
end

def test_user_can_enter_number_of_tickets
with_stdin do |user|
user.puts "2"
assert_equal(Carnival.new.get_value, "2")
end
end

# snip...
end

关于ruby - Minitest - 测试不运行 - 没有 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com