Ruby RSpec:字符串应位于数组中

Ruby RSpec:字符串应位于数组中,ruby,rspec,Ruby,Rspec,我正在测试一个随机字符串生成器,它获取两个单词数组,然后从中随机选取一个。我需要能够测试它实际上是随机的。例如: FrostyMeadow.generate(:nouns => ["hello", "world"], :adjectives => ["hello","world"] 应生成以下字符串之一: ["hello world", "world world", "world hello", "hello hello"] 有没有办法给rspec该数组并检查生成的字符串是否在其

我正在测试一个随机字符串生成器,它获取两个单词数组,然后从中随机选取一个。我需要能够测试它实际上是随机的。例如:

FrostyMeadow.generate(:nouns => ["hello", "world"], :adjectives => ["hello","world"]
应生成以下字符串之一:

["hello world", "world world", "world hello", "hello hello"]

有没有办法给rspec该数组并检查生成的字符串是否在其中?

随机性很难测试,但在您的情况下,您可以通过以下方式实现:

word = FrostyMeadow.generate(:nouns => ["hello", "world"], :adjectives => ["hello","world"]
["hello world", "world world", "world hello", "hello hello"].should include(word)

随机性很难测试,但在您的情况下,您可以通过以下方式实现:

word = FrostyMeadow.generate(:nouns => ["hello", "world"], :adjectives => ["hello","world"]
["hello world", "world world", "world hello", "hello hello"].should include(word)
另请参阅以获取一些备选答案。另请参阅以获取一些备选答案。