2013-02-28 29 views
1

我正在測試一個隨機字符串生成器,它需要兩個單詞的數組,然後從它們中隨機選擇一個。我需要能夠測試它實際上是隨機的。例如:RSpec:字符串應該在數組

FrostyMeadow.generate(:nouns => ["hello", "world"], :adjectives => ["hello","world"] 

應生成以下字符串之一:

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

有沒有什麼辦法讓rspec的數組,並檢查生成的字符串是在那裏?

+0

參見http://stackoverflow.com/questions/7922346/rspec-should-be-this-or這是一些替代答案。 – dcorking 2015-11-19 10:00:29

回答

4

隨機性是很難測試,但在你的情況下就可以實現這樣說:

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

非常感謝! – andy 2013-03-01 13:11:29