如何在Ruby中使用shuffle测试循环?
如何在Ruby中进行测试如何在Ruby中使用shuffle测试循环?,ruby,testing,Ruby,Testing,如何在Ruby中进行测试 if shuffle some_array.shuffle.each { |val| puts "#{val}" } end 我是否需要测试shuffle,因为它是一个Ruby方法?谢谢。简短回答:不 你可以相信Ruby会做正确的事情。它已经有了一个新的目标 长回答:是的 您不应该直接测试shuffle方法,而是测试您的代码是否产生正确的结果 由于您的代码使用了puts,这使得测试非常烦人。如果您可以编写一个返回可以打印的值的方法,那通常会好得多。编写代码时,请始
if shuffle
some_array.shuffle.each { |val| puts "#{val}" }
end
我是否需要测试shuffle,因为它是一个Ruby方法?谢谢。简短回答:不
你可以相信Ruby会做正确的事情。它已经有了一个新的目标
长回答:是的
您不应该直接测试shuffle
方法,而是测试您的代码是否产生正确的结果
由于您的代码使用了puts
,这使得测试非常烦人。如果您可以编写一个返回可以打印的值的方法,那通常会好得多。编写代码时,请始终考虑如何测试它
如果您正在努力解决这个问题,如果测试方法不明确,首先编写测试,然后编写代码使其通过
如果你的价值观必须被洗牌,那么你需要想出一种方法来确定它们是否被充分洗牌。这可能很困难,因为随机性是易变的。shuffle
对数据没有任何影响的可能性很小,但不是零,这就是随机性的工作原理。当列表越小,保证只使用一个元素什么也不做时,这种可能性就越大
因此,如果您可以描述为什么数据应该被洗牌,以及什么构成了良好的洗牌,那么您可以为此编写一个测试
下面是一个如何做到这一点的示例:
gem 'test-unit'
require 'test/unit'
class MyShuffler
def initialize(data)
@data = data
end
def processed
@data.map do |e|
e.downcase
end.shuffle
end
end
现在您可以这样使用:
shuffler = MyShuffler.new(%w[ a b c d e f ])
# Thin presentation layer here where we're just displaying each
# element. Test code for this is not strictly necessary.
shuffler.processed.each do |e|
puts e
end
现在,您可以单独编写数据操作的测试代码,而不是表示部分:
gem 'test-unit'
require 'test/unit'
class MyShufflerTest < Test::Unit::TestCase
def test_processed
shuffler = MyShuffler.new(%w[ A B c Dee e f Gee ])
results = shuffler.processed
expected = %w[ a b c dee e f gee ]
assert_equal expected, results.sort
assert_not_equal expected, results
counts = Hash.new(0)
iterations = 100000
# Keep track of the number of times a particular element appears in
# the first entry of the array.
iterations.times do
counts[shuffler.processed[0]] += 1
end
expected_count = iterations / expected.length
# The count for any given element should be +/- 5% versus the expected
# count. The variance generally decreases with a larger number of
# iterations.
expected.each do |e|
assert (counts[e] - expected_count).abs < iterations * 0.05
end
end
end
gem“测试单元”
需要“测试/单元”
类MyShufflerTest
简短回答:否
你可以相信Ruby会做正确的事情。它已经有了一个新的目标
长回答:是的
您不应该直接测试shuffle
方法,而是测试您的代码是否产生正确的结果
由于您的代码使用了puts
,这使得测试非常烦人。如果您可以编写一个返回可以打印的值的方法,那通常会好得多。编写代码时,请始终考虑如何测试它
如果您正在努力解决这个问题,如果测试方法不明确,首先编写测试,然后编写代码使其通过
如果你的价值观必须被洗牌,那么你需要想出一种方法来确定它们是否被充分洗牌。这可能很困难,因为随机性是易变的。shuffle
对数据没有任何影响的可能性很小,但不是零,这就是随机性的工作原理。当列表越小,保证只使用一个元素什么也不做时,这种可能性就越大
因此,如果您可以描述为什么数据应该被洗牌,以及什么构成了良好的洗牌,那么您可以为此编写一个测试
下面是一个如何做到这一点的示例:
gem 'test-unit'
require 'test/unit'
class MyShuffler
def initialize(data)
@data = data
end
def processed
@data.map do |e|
e.downcase
end.shuffle
end
end
现在您可以这样使用:
shuffler = MyShuffler.new(%w[ a b c d e f ])
# Thin presentation layer here where we're just displaying each
# element. Test code for this is not strictly necessary.
shuffler.processed.each do |e|
puts e
end
现在,您可以单独编写数据操作的测试代码,而不是表示部分:
gem 'test-unit'
require 'test/unit'
class MyShufflerTest < Test::Unit::TestCase
def test_processed
shuffler = MyShuffler.new(%w[ A B c Dee e f Gee ])
results = shuffler.processed
expected = %w[ a b c dee e f gee ]
assert_equal expected, results.sort
assert_not_equal expected, results
counts = Hash.new(0)
iterations = 100000
# Keep track of the number of times a particular element appears in
# the first entry of the array.
iterations.times do
counts[shuffler.processed[0]] += 1
end
expected_count = iterations / expected.length
# The count for any given element should be +/- 5% versus the expected
# count. The variance generally decreases with a larger number of
# iterations.
expected.each do |e|
assert (counts[e] - expected_count).abs < iterations * 0.05
end
end
end
gem“测试单元”
需要“测试/单元”
类MyShufflerTest
不应使用单元测试测试随机性。单元测试应该调用一个方法,并根据预期值测试返回值(或对象状态)。测试随机性的问题在于,对于您想要测试的大多数内容,没有一个期望值。您不应该使用单元测试来测试随机性。单元测试应该调用一个方法,并根据预期值测试返回值(或对象状态)。测试随机性的问题在于,您想测试的大多数内容都没有预期的值。我建议在代码中为内置功能编写单元测试是浪费时间。好吧。所以我根本不需要测试这个?只为您可以更改的代码编写测试。如果您使用的是内置代码,或者来自其他人的代码,并且他们编写了测试,那么就依靠他们的测试来检查他们的代码。换句话说,就像@Phrogz所说的,为Ruby的内置方法编写测试会浪费你的时间,因为它已经被测试过了。我建议在你的代码中为内置功能编写单元测试是浪费你的时间。好吧。所以我根本不需要测试这个?只为您可以更改的代码编写测试。如果您使用的是内置代码或其他人的代码,则