[rspec-users] How to test a method that return random values

David Chelimsky dchelimsky at gmail.com
Sun Jul 11 21:59:49 EDT 2010


On Jul 6, 2010, at 1:35 PM, Guilherme wrote:

> Hi,
> 
> How can I test this method using rspec ?
> 
> def self.generate_calculation number1, number2, operation
>      number1 = rand(number1)
>      number2 = rand(number2)
>      ...
> end
> 
> This method return an operation with those random numbers.
> 
> Any tip ?

Something like this:

OperationGenerator.stub(:rand).with(1).and_return(1)
OperationGenerator.stub(:rand).with(2).and_return(2)

expected = Addition.new(1,2)
actual = OperationGenerator.generate_calculation 1, 2, Addition

actual.should eq(expected)

HTH,
David



More information about the rspec-users mailing list