我正在浏览EdgeCase Ruby Koans.在about_dice_project.rb中,有一个名为"test_dice_values_should_change_between_rolls"的测试,这很简单:
def test_dice_values_should_change_between_rolls
dice = DiceSet.new
dice.roll(5)
first_time = dice.values
dice.roll(5)
second_time = dice.values
assert_not_equal first_time, second_time,
"Two rolls should not be equal"
end
Run Code Online (Sandbox Code Playgroud)
除了出现在那里的评论:
# THINK ABOUT IT:
#
# If the rolls are random, then it is possible (although not
# likely) that two consecutive rolls are equal. What would be a
# better way to test this.
Run Code Online (Sandbox Code Playgroud)
哪个(显然)让我思考:什么是可靠地测试随机事物的最佳方式(特别是,通常)?
我今天下午碰到了这种情况,所以我想我会问你们做了什么.
我们有一个用于用户密码重置的随机密码生成器,在修复问题时,我决定将例程移到我的(慢慢增长的)测试工具中.
我想测试生成的密码是否符合我们设定的规则,但当然函数的结果将是随机的(或者,伪随机化).
你们在单元测试中会做些什么?生成一堆密码,检查它们是否全部通过,并认为它足够好?