How would I go about testing the following helper method?
module HypotheticalHelper
def next(array)
array.sample
end
end
I have this:
helper.next([1,2]).should_not == helper.next([1,2])
Is there a better technique than repeating this a statistically significant number of times?
What behavior are you trying to test? That you get random elements from the array? If so, then just run the test until the same call returns an element that doesn’t match the last run.
How would this be used? If you want random access to an array, why not simply
array.shuffle!and then iterate the array?Array#samplecan give consecutive equivalent results; it’s expected that it will. If you want to never have the same results concurrently, then you’ll have to maintain state and loop until you get a result that wasn’t the last result, or you’ll want to use something like my shuffle suggestion (which will never give repeating results).