andy andy - 2 years ago 98
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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download