andy andy - 10 months ago 40
Ruby Question

RSpec: String should be in array

I am testing a random string generator that takes two arrays of words and then picks random ones from them. I need to be able to test that it actually is random. For instance:

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

should generate one of the following strings:

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

Is there any way to give rspec that array and check if the generated string is in there?


Randomness is hard to test, but in your case you can achieve it this way:

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