Minitest - 测试不 运行 - 否 Rails
Minitest - Tests Don't Run - No Rails
我刚刚开始一个模拟嘉年华售票亭的小项目,其中一项准则是测试用户是否可以输入门票数量。由于 @Stefan 在 this question.
上的回答,控制台中的程序 运行s 最终(希望)弄清楚了如何实施此测试
问题是现在,当我 运行 测试文件时,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
如果有人能帮助弄清楚为什么测试不是 运行ning,我将不胜感激!
按照惯例,Minitest 中的测试是 public 以 test_
开头的实例方法,因此原始测试没有实际的测试方法。您需要更新您的测试 class 以便带有断言的方法遵循如下约定:
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
是的,总是用 test_ 开始你的所有测试,所以它知道你想要那个 function/method
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
这应该适合你
我刚刚开始一个模拟嘉年华售票亭的小项目,其中一项准则是测试用户是否可以输入门票数量。由于 @Stefan 在 this question.
上的回答,控制台中的程序 运行s 最终(希望)弄清楚了如何实施此测试问题是现在,当我 运行 测试文件时,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
如果有人能帮助弄清楚为什么测试不是 运行ning,我将不胜感激!
按照惯例,Minitest 中的测试是 public 以 test_
开头的实例方法,因此原始测试没有实际的测试方法。您需要更新您的测试 class 以便带有断言的方法遵循如下约定:
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
是的,总是用 test_ 开始你的所有测试,所以它知道你想要那个 function/method
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
这应该适合你